{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 263,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv('datasets/CDNOW.txt',header=None,sep=r'\\s+',names=['user_id','buy_date','count','price'])\n",
    "data['buy_date'] = pd.to_datetime(data['buy_date'].astype(str))\n",
    "data.set_index('buy_date',inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 264,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(69659, 3)"
      ]
     },
     "execution_count": 264,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 265,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>count</th>\n",
       "      <th>price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buy_date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1997-01-01</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>11.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01-12</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>12.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01-12</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>77.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01-02</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-03-30</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            user_id  count  price\n",
       "buy_date                         \n",
       "1997-01-01        1      1  11.77\n",
       "1997-01-12        2      1  12.00\n",
       "1997-01-12        2      5  77.00\n",
       "1997-01-02        3      2  20.76\n",
       "1997-03-30        3      2  20.76"
      ]
     },
     "execution_count": 265,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 266,
   "metadata": {},
   "outputs": [],
   "source": [
    "data['single_price'] = data['price']/data['count']\n",
    "data_desc = data[['count','single_price']].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>single_price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>69659.000000</td>\n",
       "      <td>69659.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>2.410040</td>\n",
       "      <td>15.459447</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>2.333924</td>\n",
       "      <td>8.735528</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>12.270000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2.000000</td>\n",
       "      <td>13.990000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>3.000000</td>\n",
       "      <td>15.490000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>99.000000</td>\n",
       "      <td>305.990000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              count  single_price\n",
       "count  69659.000000  69659.000000\n",
       "mean       2.410040     15.459447\n",
       "std        2.333924      8.735528\n",
       "min        1.000000      0.000000\n",
       "25%        1.000000     12.270000\n",
       "50%        2.000000     13.990000\n",
       "75%        3.000000     15.490000\n",
       "max       99.000000    305.990000"
      ]
     },
     "execution_count": 267,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_desc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 268,
   "metadata": {},
   "outputs": [],
   "source": [
    "single_price_lower= data_desc.single_price['mean'] - 1.96 * data_desc.single_price['std']\n",
    "single_price_lower = single_price_lower if single_price_lower>0 else 0\n",
    "single_price_upper = data_desc.single_price['mean'] + 1.96 * data_desc.single_price['std']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(32.58108178364721, 0)"
      ]
     },
     "execution_count": 269,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "single_price_upper,single_price_lower"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 271,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(67663, 4)"
      ]
     },
     "execution_count": 271,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = data[data.single_price < single_price_upper]\n",
    "a = a[a.single_price>single_price_lower]\n",
    "data = a\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 272,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "/* Put everything inside the global mpl namespace */\n",
       "window.mpl = {};\n",
       "\n",
       "\n",
       "mpl.get_websocket_type = function() {\n",
       "    if (typeof(WebSocket) !== 'undefined') {\n",
       "        return WebSocket;\n",
       "    } else if (typeof(MozWebSocket) !== 'undefined') {\n",
       "        return MozWebSocket;\n",
       "    } else {\n",
       "        alert('Your browser does not have WebSocket support.' +\n",
       "              'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
       "              'Firefox 4 and 5 are also supported but you ' +\n",
       "              'have to enable WebSockets in about:config.');\n",
       "    };\n",
       "}\n",
       "\n",
       "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
       "    this.id = figure_id;\n",
       "\n",
       "    this.ws = websocket;\n",
       "\n",
       "    this.supports_binary = (this.ws.binaryType != undefined);\n",
       "\n",
       "    if (!this.supports_binary) {\n",
       "        var warnings = document.getElementById(\"mpl-warnings\");\n",
       "        if (warnings) {\n",
       "            warnings.style.display = 'block';\n",
       "            warnings.textContent = (\n",
       "                \"This browser does not support binary websocket messages. \" +\n",
       "                    \"Performance may be slow.\");\n",
       "        }\n",
       "    }\n",
       "\n",
       "    this.imageObj = new Image();\n",
       "\n",
       "    this.context = undefined;\n",
       "    this.message = undefined;\n",
       "    this.canvas = undefined;\n",
       "    this.rubberband_canvas = undefined;\n",
       "    this.rubberband_context = undefined;\n",
       "    this.format_dropdown = undefined;\n",
       "\n",
       "    this.image_mode = 'full';\n",
       "\n",
       "    this.root = $('<div/>');\n",
       "    this._root_extra_style(this.root)\n",
       "    this.root.attr('style', 'display: inline-block');\n",
       "\n",
       "    $(parent_element).append(this.root);\n",
       "\n",
       "    this._init_header(this);\n",
       "    this._init_canvas(this);\n",
       "    this._init_toolbar(this);\n",
       "\n",
       "    var fig = this;\n",
       "\n",
       "    this.waiting = false;\n",
       "\n",
       "    this.ws.onopen =  function () {\n",
       "            fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
       "            fig.send_message(\"send_image_mode\", {});\n",
       "            if (mpl.ratio != 1) {\n",
       "                fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
       "            }\n",
       "            fig.send_message(\"refresh\", {});\n",
       "        }\n",
       "\n",
       "    this.imageObj.onload = function() {\n",
       "            if (fig.image_mode == 'full') {\n",
       "                // Full images could contain transparency (where diff images\n",
       "                // almost always do), so we need to clear the canvas so that\n",
       "                // there is no ghosting.\n",
       "                fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
       "            }\n",
       "            fig.context.drawImage(fig.imageObj, 0, 0);\n",
       "        };\n",
       "\n",
       "    this.imageObj.onunload = function() {\n",
       "        fig.ws.close();\n",
       "    }\n",
       "\n",
       "    this.ws.onmessage = this._make_on_message_function(this);\n",
       "\n",
       "    this.ondownload = ondownload;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_header = function() {\n",
       "    var titlebar = $(\n",
       "        '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
       "        'ui-helper-clearfix\"/>');\n",
       "    var titletext = $(\n",
       "        '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
       "        'text-align: center; padding: 3px;\"/>');\n",
       "    titlebar.append(titletext)\n",
       "    this.root.append(titlebar);\n",
       "    this.header = titletext[0];\n",
       "}\n",
       "\n",
       "\n",
       "\n",
       "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
       "\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
       "\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_canvas = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var canvas_div = $('<div/>');\n",
       "\n",
       "    canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
       "\n",
       "    function canvas_keyboard_event(event) {\n",
       "        return fig.key_event(event, event['data']);\n",
       "    }\n",
       "\n",
       "    canvas_div.keydown('key_press', canvas_keyboard_event);\n",
       "    canvas_div.keyup('key_release', canvas_keyboard_event);\n",
       "    this.canvas_div = canvas_div\n",
       "    this._canvas_extra_style(canvas_div)\n",
       "    this.root.append(canvas_div);\n",
       "\n",
       "    var canvas = $('<canvas/>');\n",
       "    canvas.addClass('mpl-canvas');\n",
       "    canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
       "\n",
       "    this.canvas = canvas[0];\n",
       "    this.context = canvas[0].getContext(\"2d\");\n",
       "\n",
       "    var backingStore = this.context.backingStorePixelRatio ||\n",
       "\tthis.context.webkitBackingStorePixelRatio ||\n",
       "\tthis.context.mozBackingStorePixelRatio ||\n",
       "\tthis.context.msBackingStorePixelRatio ||\n",
       "\tthis.context.oBackingStorePixelRatio ||\n",
       "\tthis.context.backingStorePixelRatio || 1;\n",
       "\n",
       "    mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
       "\n",
       "    var rubberband = $('<canvas/>');\n",
       "    rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
       "\n",
       "    var pass_mouse_events = true;\n",
       "\n",
       "    canvas_div.resizable({\n",
       "        start: function(event, ui) {\n",
       "            pass_mouse_events = false;\n",
       "        },\n",
       "        resize: function(event, ui) {\n",
       "            fig.request_resize(ui.size.width, ui.size.height);\n",
       "        },\n",
       "        stop: function(event, ui) {\n",
       "            pass_mouse_events = true;\n",
       "            fig.request_resize(ui.size.width, ui.size.height);\n",
       "        },\n",
       "    });\n",
       "\n",
       "    function mouse_event_fn(event) {\n",
       "        if (pass_mouse_events)\n",
       "            return fig.mouse_event(event, event['data']);\n",
       "    }\n",
       "\n",
       "    rubberband.mousedown('button_press', mouse_event_fn);\n",
       "    rubberband.mouseup('button_release', mouse_event_fn);\n",
       "    // Throttle sequential mouse events to 1 every 20ms.\n",
       "    rubberband.mousemove('motion_notify', mouse_event_fn);\n",
       "\n",
       "    rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
       "    rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
       "\n",
       "    canvas_div.on(\"wheel\", function (event) {\n",
       "        event = event.originalEvent;\n",
       "        event['data'] = 'scroll'\n",
       "        if (event.deltaY < 0) {\n",
       "            event.step = 1;\n",
       "        } else {\n",
       "            event.step = -1;\n",
       "        }\n",
       "        mouse_event_fn(event);\n",
       "    });\n",
       "\n",
       "    canvas_div.append(canvas);\n",
       "    canvas_div.append(rubberband);\n",
       "\n",
       "    this.rubberband = rubberband;\n",
       "    this.rubberband_canvas = rubberband[0];\n",
       "    this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
       "    this.rubberband_context.strokeStyle = \"#000000\";\n",
       "\n",
       "    this._resize_canvas = function(width, height) {\n",
       "        // Keep the size of the canvas, canvas container, and rubber band\n",
       "        // canvas in synch.\n",
       "        canvas_div.css('width', width)\n",
       "        canvas_div.css('height', height)\n",
       "\n",
       "        canvas.attr('width', width * mpl.ratio);\n",
       "        canvas.attr('height', height * mpl.ratio);\n",
       "        canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
       "\n",
       "        rubberband.attr('width', width);\n",
       "        rubberband.attr('height', height);\n",
       "    }\n",
       "\n",
       "    // Set the figure to an initial 600x600px, this will subsequently be updated\n",
       "    // upon first draw.\n",
       "    this._resize_canvas(600, 600);\n",
       "\n",
       "    // Disable right mouse context menu.\n",
       "    $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
       "        return false;\n",
       "    });\n",
       "\n",
       "    function set_focus () {\n",
       "        canvas.focus();\n",
       "        canvas_div.focus();\n",
       "    }\n",
       "\n",
       "    window.setTimeout(set_focus, 100);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_toolbar = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var nav_element = $('<div/>')\n",
       "    nav_element.attr('style', 'width: 100%');\n",
       "    this.root.append(nav_element);\n",
       "\n",
       "    // Define a callback function for later on.\n",
       "    function toolbar_event(event) {\n",
       "        return fig.toolbar_button_onclick(event['data']);\n",
       "    }\n",
       "    function toolbar_mouse_event(event) {\n",
       "        return fig.toolbar_button_onmouseover(event['data']);\n",
       "    }\n",
       "\n",
       "    for(var toolbar_ind in mpl.toolbar_items) {\n",
       "        var name = mpl.toolbar_items[toolbar_ind][0];\n",
       "        var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
       "        var image = mpl.toolbar_items[toolbar_ind][2];\n",
       "        var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
       "\n",
       "        if (!name) {\n",
       "            // put a spacer in here.\n",
       "            continue;\n",
       "        }\n",
       "        var button = $('<button/>');\n",
       "        button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
       "                        'ui-button-icon-only');\n",
       "        button.attr('role', 'button');\n",
       "        button.attr('aria-disabled', 'false');\n",
       "        button.click(method_name, toolbar_event);\n",
       "        button.mouseover(tooltip, toolbar_mouse_event);\n",
       "\n",
       "        var icon_img = $('<span/>');\n",
       "        icon_img.addClass('ui-button-icon-primary ui-icon');\n",
       "        icon_img.addClass(image);\n",
       "        icon_img.addClass('ui-corner-all');\n",
       "\n",
       "        var tooltip_span = $('<span/>');\n",
       "        tooltip_span.addClass('ui-button-text');\n",
       "        tooltip_span.html(tooltip);\n",
       "\n",
       "        button.append(icon_img);\n",
       "        button.append(tooltip_span);\n",
       "\n",
       "        nav_element.append(button);\n",
       "    }\n",
       "\n",
       "    var fmt_picker_span = $('<span/>');\n",
       "\n",
       "    var fmt_picker = $('<select/>');\n",
       "    fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
       "    fmt_picker_span.append(fmt_picker);\n",
       "    nav_element.append(fmt_picker_span);\n",
       "    this.format_dropdown = fmt_picker[0];\n",
       "\n",
       "    for (var ind in mpl.extensions) {\n",
       "        var fmt = mpl.extensions[ind];\n",
       "        var option = $(\n",
       "            '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
       "        fmt_picker.append(option)\n",
       "    }\n",
       "\n",
       "    // Add hover states to the ui-buttons\n",
       "    $( \".ui-button\" ).hover(\n",
       "        function() { $(this).addClass(\"ui-state-hover\");},\n",
       "        function() { $(this).removeClass(\"ui-state-hover\");}\n",
       "    );\n",
       "\n",
       "    var status_bar = $('<span class=\"mpl-message\"/>');\n",
       "    nav_element.append(status_bar);\n",
       "    this.message = status_bar[0];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
       "    // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
       "    // which will in turn request a refresh of the image.\n",
       "    this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.send_message = function(type, properties) {\n",
       "    properties['type'] = type;\n",
       "    properties['figure_id'] = this.id;\n",
       "    this.ws.send(JSON.stringify(properties));\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.send_draw_message = function() {\n",
       "    if (!this.waiting) {\n",
       "        this.waiting = true;\n",
       "        this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
       "    }\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
       "    var format_dropdown = fig.format_dropdown;\n",
       "    var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
       "    fig.ondownload(fig, format);\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
       "    var size = msg['size'];\n",
       "    if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
       "        fig._resize_canvas(size[0], size[1]);\n",
       "        fig.send_message(\"refresh\", {});\n",
       "    };\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
       "    var x0 = msg['x0'] / mpl.ratio;\n",
       "    var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
       "    var x1 = msg['x1'] / mpl.ratio;\n",
       "    var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
       "    x0 = Math.floor(x0) + 0.5;\n",
       "    y0 = Math.floor(y0) + 0.5;\n",
       "    x1 = Math.floor(x1) + 0.5;\n",
       "    y1 = Math.floor(y1) + 0.5;\n",
       "    var min_x = Math.min(x0, x1);\n",
       "    var min_y = Math.min(y0, y1);\n",
       "    var width = Math.abs(x1 - x0);\n",
       "    var height = Math.abs(y1 - y0);\n",
       "\n",
       "    fig.rubberband_context.clearRect(\n",
       "        0, 0, fig.canvas.width, fig.canvas.height);\n",
       "\n",
       "    fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
       "    // Updates the figure title.\n",
       "    fig.header.textContent = msg['label'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
       "    var cursor = msg['cursor'];\n",
       "    switch(cursor)\n",
       "    {\n",
       "    case 0:\n",
       "        cursor = 'pointer';\n",
       "        break;\n",
       "    case 1:\n",
       "        cursor = 'default';\n",
       "        break;\n",
       "    case 2:\n",
       "        cursor = 'crosshair';\n",
       "        break;\n",
       "    case 3:\n",
       "        cursor = 'move';\n",
       "        break;\n",
       "    }\n",
       "    fig.rubberband_canvas.style.cursor = cursor;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_message = function(fig, msg) {\n",
       "    fig.message.textContent = msg['message'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
       "    // Request the server to send over a new figure.\n",
       "    fig.send_draw_message();\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
       "    fig.image_mode = msg['mode'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.updated_canvas_event = function() {\n",
       "    // Called whenever the canvas gets updated.\n",
       "    this.send_message(\"ack\", {});\n",
       "}\n",
       "\n",
       "// A function to construct a web socket function for onmessage handling.\n",
       "// Called in the figure constructor.\n",
       "mpl.figure.prototype._make_on_message_function = function(fig) {\n",
       "    return function socket_on_message(evt) {\n",
       "        if (evt.data instanceof Blob) {\n",
       "            /* FIXME: We get \"Resource interpreted as Image but\n",
       "             * transferred with MIME type text/plain:\" errors on\n",
       "             * Chrome.  But how to set the MIME type?  It doesn't seem\n",
       "             * to be part of the websocket stream */\n",
       "            evt.data.type = \"image/png\";\n",
       "\n",
       "            /* Free the memory for the previous frames */\n",
       "            if (fig.imageObj.src) {\n",
       "                (window.URL || window.webkitURL).revokeObjectURL(\n",
       "                    fig.imageObj.src);\n",
       "            }\n",
       "\n",
       "            fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
       "                evt.data);\n",
       "            fig.updated_canvas_event();\n",
       "            fig.waiting = false;\n",
       "            return;\n",
       "        }\n",
       "        else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
       "            fig.imageObj.src = evt.data;\n",
       "            fig.updated_canvas_event();\n",
       "            fig.waiting = false;\n",
       "            return;\n",
       "        }\n",
       "\n",
       "        var msg = JSON.parse(evt.data);\n",
       "        var msg_type = msg['type'];\n",
       "\n",
       "        // Call the  \"handle_{type}\" callback, which takes\n",
       "        // the figure and JSON message as its only arguments.\n",
       "        try {\n",
       "            var callback = fig[\"handle_\" + msg_type];\n",
       "        } catch (e) {\n",
       "            console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
       "            return;\n",
       "        }\n",
       "\n",
       "        if (callback) {\n",
       "            try {\n",
       "                // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
       "                callback(fig, msg);\n",
       "            } catch (e) {\n",
       "                console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
       "            }\n",
       "        }\n",
       "    };\n",
       "}\n",
       "\n",
       "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
       "mpl.findpos = function(e) {\n",
       "    //this section is from http://www.quirksmode.org/js/events_properties.html\n",
       "    var targ;\n",
       "    if (!e)\n",
       "        e = window.event;\n",
       "    if (e.target)\n",
       "        targ = e.target;\n",
       "    else if (e.srcElement)\n",
       "        targ = e.srcElement;\n",
       "    if (targ.nodeType == 3) // defeat Safari bug\n",
       "        targ = targ.parentNode;\n",
       "\n",
       "    // jQuery normalizes the pageX and pageY\n",
       "    // pageX,Y are the mouse positions relative to the document\n",
       "    // offset() returns the position of the element relative to the document\n",
       "    var x = e.pageX - $(targ).offset().left;\n",
       "    var y = e.pageY - $(targ).offset().top;\n",
       "\n",
       "    return {\"x\": x, \"y\": y};\n",
       "};\n",
       "\n",
       "/*\n",
       " * return a copy of an object with only non-object keys\n",
       " * we need this to avoid circular references\n",
       " * http://stackoverflow.com/a/24161582/3208463\n",
       " */\n",
       "function simpleKeys (original) {\n",
       "  return Object.keys(original).reduce(function (obj, key) {\n",
       "    if (typeof original[key] !== 'object')\n",
       "        obj[key] = original[key]\n",
       "    return obj;\n",
       "  }, {});\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.mouse_event = function(event, name) {\n",
       "    var canvas_pos = mpl.findpos(event)\n",
       "\n",
       "    if (name === 'button_press')\n",
       "    {\n",
       "        this.canvas.focus();\n",
       "        this.canvas_div.focus();\n",
       "    }\n",
       "\n",
       "    var x = canvas_pos.x * mpl.ratio;\n",
       "    var y = canvas_pos.y * mpl.ratio;\n",
       "\n",
       "    this.send_message(name, {x: x, y: y, button: event.button,\n",
       "                             step: event.step,\n",
       "                             guiEvent: simpleKeys(event)});\n",
       "\n",
       "    /* This prevents the web browser from automatically changing to\n",
       "     * the text insertion cursor when the button is pressed.  We want\n",
       "     * to control all of the cursor setting manually through the\n",
       "     * 'cursor' event from matplotlib */\n",
       "    event.preventDefault();\n",
       "    return false;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
       "    // Handle any extra behaviour associated with a key event\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.key_event = function(event, name) {\n",
       "\n",
       "    // Prevent repeat events\n",
       "    if (name == 'key_press')\n",
       "    {\n",
       "        if (event.which === this._key)\n",
       "            return;\n",
       "        else\n",
       "            this._key = event.which;\n",
       "    }\n",
       "    if (name == 'key_release')\n",
       "        this._key = null;\n",
       "\n",
       "    var value = '';\n",
       "    if (event.ctrlKey && event.which != 17)\n",
       "        value += \"ctrl+\";\n",
       "    if (event.altKey && event.which != 18)\n",
       "        value += \"alt+\";\n",
       "    if (event.shiftKey && event.which != 16)\n",
       "        value += \"shift+\";\n",
       "\n",
       "    value += 'k';\n",
       "    value += event.which.toString();\n",
       "\n",
       "    this._key_event_extra(event, name);\n",
       "\n",
       "    this.send_message(name, {key: value,\n",
       "                             guiEvent: simpleKeys(event)});\n",
       "    return false;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
       "    if (name == 'download') {\n",
       "        this.handle_save(this, null);\n",
       "    } else {\n",
       "        this.send_message(\"toolbar_button\", {name: name});\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
       "    this.message.textContent = tooltip;\n",
       "};\n",
       "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to  previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
       "\n",
       "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
       "\n",
       "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
       "    // Create a \"websocket\"-like object which calls the given IPython comm\n",
       "    // object with the appropriate methods. Currently this is a non binary\n",
       "    // socket, so there is still some room for performance tuning.\n",
       "    var ws = {};\n",
       "\n",
       "    ws.close = function() {\n",
       "        comm.close()\n",
       "    };\n",
       "    ws.send = function(m) {\n",
       "        //console.log('sending', m);\n",
       "        comm.send(m);\n",
       "    };\n",
       "    // Register the callback with on_msg.\n",
       "    comm.on_msg(function(msg) {\n",
       "        //console.log('receiving', msg['content']['data'], msg);\n",
       "        // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
       "        ws.onmessage(msg['content']['data'])\n",
       "    });\n",
       "    return ws;\n",
       "}\n",
       "\n",
       "mpl.mpl_figure_comm = function(comm, msg) {\n",
       "    // This is the function which gets called when the mpl process\n",
       "    // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
       "\n",
       "    var id = msg.content.data.id;\n",
       "    // Get hold of the div created by the display call when the Comm\n",
       "    // socket was opened in Python.\n",
       "    var element = $(\"#\" + id);\n",
       "    var ws_proxy = comm_websocket_adapter(comm)\n",
       "\n",
       "    function ondownload(figure, format) {\n",
       "        window.open(figure.imageObj.src);\n",
       "    }\n",
       "\n",
       "    var fig = new mpl.figure(id, ws_proxy,\n",
       "                           ondownload,\n",
       "                           element.get(0));\n",
       "\n",
       "    // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
       "    // web socket which is closed, not our websocket->open comm proxy.\n",
       "    ws_proxy.onopen();\n",
       "\n",
       "    fig.parent_element = element.get(0);\n",
       "    fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
       "    if (!fig.cell_info) {\n",
       "        console.error(\"Failed to find cell for figure\", id, fig);\n",
       "        return;\n",
       "    }\n",
       "\n",
       "    var output_index = fig.cell_info[2]\n",
       "    var cell = fig.cell_info[0];\n",
       "\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_close = function(fig, msg) {\n",
       "    var width = fig.canvas.width/mpl.ratio\n",
       "    fig.root.unbind('remove')\n",
       "\n",
       "    // Update the output cell to use the data from the current canvas.\n",
       "    fig.push_to_output();\n",
       "    var dataURL = fig.canvas.toDataURL();\n",
       "    // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
       "    // the notebook keyboard shortcuts fail.\n",
       "    IPython.keyboard_manager.enable()\n",
       "    $(fig.parent_element).html('<img src=\"' + dataURL + '\" width=\"' + width + '\">');\n",
       "    fig.close_ws(fig, msg);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.close_ws = function(fig, msg){\n",
       "    fig.send_message('closing', msg);\n",
       "    // fig.ws.close()\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
       "    // Turn the data on the canvas into data in the output cell.\n",
       "    var width = this.canvas.width/mpl.ratio\n",
       "    var dataURL = this.canvas.toDataURL();\n",
       "    this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.updated_canvas_event = function() {\n",
       "    // Tell IPython that the notebook contents must change.\n",
       "    IPython.notebook.set_dirty(true);\n",
       "    this.send_message(\"ack\", {});\n",
       "    var fig = this;\n",
       "    // Wait a second, then push the new image to the DOM so\n",
       "    // that it is saved nicely (might be nice to debounce this).\n",
       "    setTimeout(function () { fig.push_to_output() }, 1000);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_toolbar = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var nav_element = $('<div/>')\n",
       "    nav_element.attr('style', 'width: 100%');\n",
       "    this.root.append(nav_element);\n",
       "\n",
       "    // Define a callback function for later on.\n",
       "    function toolbar_event(event) {\n",
       "        return fig.toolbar_button_onclick(event['data']);\n",
       "    }\n",
       "    function toolbar_mouse_event(event) {\n",
       "        return fig.toolbar_button_onmouseover(event['data']);\n",
       "    }\n",
       "\n",
       "    for(var toolbar_ind in mpl.toolbar_items){\n",
       "        var name = mpl.toolbar_items[toolbar_ind][0];\n",
       "        var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
       "        var image = mpl.toolbar_items[toolbar_ind][2];\n",
       "        var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
       "\n",
       "        if (!name) { continue; };\n",
       "\n",
       "        var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
       "        button.click(method_name, toolbar_event);\n",
       "        button.mouseover(tooltip, toolbar_mouse_event);\n",
       "        nav_element.append(button);\n",
       "    }\n",
       "\n",
       "    // Add the status bar.\n",
       "    var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
       "    nav_element.append(status_bar);\n",
       "    this.message = status_bar[0];\n",
       "\n",
       "    // Add the close button to the window.\n",
       "    var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
       "    var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
       "    button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
       "    button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
       "    buttongrp.append(button);\n",
       "    var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
       "    titlebar.prepend(buttongrp);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._root_extra_style = function(el){\n",
       "    var fig = this\n",
       "    el.on(\"remove\", function(){\n",
       "\tfig.close_ws(fig, {});\n",
       "    });\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._canvas_extra_style = function(el){\n",
       "    // this is important to make the div 'focusable\n",
       "    el.attr('tabindex', 0)\n",
       "    // reach out to IPython and tell the keyboard manager to turn it's self\n",
       "    // off when our div gets focus\n",
       "\n",
       "    // location in version 3\n",
       "    if (IPython.notebook.keyboard_manager) {\n",
       "        IPython.notebook.keyboard_manager.register_events(el);\n",
       "    }\n",
       "    else {\n",
       "        // location in version 2\n",
       "        IPython.keyboard_manager.register_events(el);\n",
       "    }\n",
       "\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
       "    var manager = IPython.notebook.keyboard_manager;\n",
       "    if (!manager)\n",
       "        manager = IPython.keyboard_manager;\n",
       "\n",
       "    // Check for shift+enter\n",
       "    if (event.shiftKey && event.which == 13) {\n",
       "        this.canvas_div.blur();\n",
       "        event.shiftKey = false;\n",
       "        // Send a \"J\" for go to next cell\n",
       "        event.which = 74;\n",
       "        event.keyCode = 74;\n",
       "        manager.command_mode();\n",
       "        manager.handle_keydown(event);\n",
       "    }\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
       "    fig.ondownload(fig, null);\n",
       "}\n",
       "\n",
       "\n",
       "mpl.find_output_cell = function(html_output) {\n",
       "    // Return the cell and output element which can be found *uniquely* in the notebook.\n",
       "    // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
       "    // IPython event is triggered only after the cells have been serialised, which for\n",
       "    // our purposes (turning an active figure into a static one), is too late.\n",
       "    var cells = IPython.notebook.get_cells();\n",
       "    var ncells = cells.length;\n",
       "    for (var i=0; i<ncells; i++) {\n",
       "        var cell = cells[i];\n",
       "        if (cell.cell_type === 'code'){\n",
       "            for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
       "                var data = cell.output_area.outputs[j];\n",
       "                if (data.data) {\n",
       "                    // IPython >= 3 moved mimebundle to data attribute of output\n",
       "                    data = data.data;\n",
       "                }\n",
       "                if (data['text/html'] == html_output) {\n",
       "                    return [cell, data, j];\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    }\n",
       "}\n",
       "\n",
       "// Register the function which deals with the matplotlib target/channel.\n",
       "// The kernel may be null if the page has been refreshed.\n",
       "if (IPython.notebook.kernel != null) {\n",
       "    IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
       "}\n"
      ],
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<img src=\"\" width=\"640\">"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig,ax = plt.subplots(2,1,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 274,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x24643e099b0>"
      ]
     },
     "execution_count": 274,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby('buy_date')['single_price'].mean().plot(ax=ax[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 275,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x2464a7e0c18>"
      ]
     },
     "execution_count": 275,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.groupby('buy_date')['count'].sum().plot(ax=ax[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 276,
   "metadata": {},
   "outputs": [],
   "source": [
    "uGp = data.groupby('user_id')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 277,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "item_count = uGp['count'].agg(['sum','max']).sort_values(by='sum',ascending=False)\n",
    "item_count['count_cum'] = item_count['sum'].cumsum()/item_count['sum'].sum()\n",
    "item_count['max_rate'] = item_count['max']/item_count['sum']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 278,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum</th>\n",
       "      <th>max</th>\n",
       "      <th>count_cum</th>\n",
       "      <th>max_rate</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>14048</th>\n",
       "      <td>1033</td>\n",
       "      <td>15</td>\n",
       "      <td>0.006260</td>\n",
       "      <td>0.014521</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7592</th>\n",
       "      <td>897</td>\n",
       "      <td>47</td>\n",
       "      <td>0.011696</td>\n",
       "      <td>0.052397</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7983</th>\n",
       "      <td>536</td>\n",
       "      <td>23</td>\n",
       "      <td>0.014944</td>\n",
       "      <td>0.042910</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7931</th>\n",
       "      <td>512</td>\n",
       "      <td>21</td>\n",
       "      <td>0.018046</td>\n",
       "      <td>0.041016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19339</th>\n",
       "      <td>368</td>\n",
       "      <td>24</td>\n",
       "      <td>0.020276</td>\n",
       "      <td>0.065217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22279</th>\n",
       "      <td>330</td>\n",
       "      <td>63</td>\n",
       "      <td>0.022276</td>\n",
       "      <td>0.190909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499</th>\n",
       "      <td>326</td>\n",
       "      <td>21</td>\n",
       "      <td>0.024252</td>\n",
       "      <td>0.064417</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6569</th>\n",
       "      <td>315</td>\n",
       "      <td>18</td>\n",
       "      <td>0.026160</td>\n",
       "      <td>0.057143</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3049</th>\n",
       "      <td>300</td>\n",
       "      <td>17</td>\n",
       "      <td>0.027978</td>\n",
       "      <td>0.056667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16517</th>\n",
       "      <td>298</td>\n",
       "      <td>18</td>\n",
       "      <td>0.029784</td>\n",
       "      <td>0.060403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13167</th>\n",
       "      <td>292</td>\n",
       "      <td>21</td>\n",
       "      <td>0.031554</td>\n",
       "      <td>0.071918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12367</th>\n",
       "      <td>263</td>\n",
       "      <td>8</td>\n",
       "      <td>0.033147</td>\n",
       "      <td>0.030418</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22061</th>\n",
       "      <td>263</td>\n",
       "      <td>13</td>\n",
       "      <td>0.034741</td>\n",
       "      <td>0.049430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2664</th>\n",
       "      <td>251</td>\n",
       "      <td>24</td>\n",
       "      <td>0.036262</td>\n",
       "      <td>0.095618</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20917</th>\n",
       "      <td>221</td>\n",
       "      <td>10</td>\n",
       "      <td>0.037602</td>\n",
       "      <td>0.045249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21950</th>\n",
       "      <td>204</td>\n",
       "      <td>14</td>\n",
       "      <td>0.038838</td>\n",
       "      <td>0.068627</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22348</th>\n",
       "      <td>195</td>\n",
       "      <td>24</td>\n",
       "      <td>0.040019</td>\n",
       "      <td>0.123077</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2908</th>\n",
       "      <td>187</td>\n",
       "      <td>14</td>\n",
       "      <td>0.041153</td>\n",
       "      <td>0.074866</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>586</th>\n",
       "      <td>183</td>\n",
       "      <td>15</td>\n",
       "      <td>0.042262</td>\n",
       "      <td>0.081967</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16175</th>\n",
       "      <td>182</td>\n",
       "      <td>29</td>\n",
       "      <td>0.043364</td>\n",
       "      <td>0.159341</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21739</th>\n",
       "      <td>179</td>\n",
       "      <td>18</td>\n",
       "      <td>0.044449</td>\n",
       "      <td>0.100559</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>313</th>\n",
       "      <td>174</td>\n",
       "      <td>30</td>\n",
       "      <td>0.045504</td>\n",
       "      <td>0.172414</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3901</th>\n",
       "      <td>174</td>\n",
       "      <td>8</td>\n",
       "      <td>0.046558</td>\n",
       "      <td>0.045977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19597</th>\n",
       "      <td>173</td>\n",
       "      <td>6</td>\n",
       "      <td>0.047606</td>\n",
       "      <td>0.034682</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22492</th>\n",
       "      <td>169</td>\n",
       "      <td>22</td>\n",
       "      <td>0.048630</td>\n",
       "      <td>0.130178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17337</th>\n",
       "      <td>164</td>\n",
       "      <td>32</td>\n",
       "      <td>0.049624</td>\n",
       "      <td>0.195122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21694</th>\n",
       "      <td>163</td>\n",
       "      <td>11</td>\n",
       "      <td>0.050612</td>\n",
       "      <td>0.067485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15162</th>\n",
       "      <td>161</td>\n",
       "      <td>13</td>\n",
       "      <td>0.051588</td>\n",
       "      <td>0.080745</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8250</th>\n",
       "      <td>160</td>\n",
       "      <td>20</td>\n",
       "      <td>0.052557</td>\n",
       "      <td>0.125000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1077</th>\n",
       "      <td>158</td>\n",
       "      <td>12</td>\n",
       "      <td>0.053515</td>\n",
       "      <td>0.075949</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15249</th>\n",
       "      <td>17</td>\n",
       "      <td>9</td>\n",
       "      <td>0.497000</td>\n",
       "      <td>0.529412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20763</th>\n",
       "      <td>17</td>\n",
       "      <td>9</td>\n",
       "      <td>0.497103</td>\n",
       "      <td>0.529412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2999</th>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>0.497206</td>\n",
       "      <td>0.176471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2998</th>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>0.497309</td>\n",
       "      <td>0.235294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7600</th>\n",
       "      <td>17</td>\n",
       "      <td>5</td>\n",
       "      <td>0.497412</td>\n",
       "      <td>0.294118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12816</th>\n",
       "      <td>17</td>\n",
       "      <td>9</td>\n",
       "      <td>0.497515</td>\n",
       "      <td>0.529412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>82</th>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>0.497618</td>\n",
       "      <td>0.176471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2044</th>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>0.497721</td>\n",
       "      <td>0.176471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20831</th>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>0.497825</td>\n",
       "      <td>0.235294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17988</th>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>0.497928</td>\n",
       "      <td>0.235294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13194</th>\n",
       "      <td>17</td>\n",
       "      <td>6</td>\n",
       "      <td>0.498031</td>\n",
       "      <td>0.352941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15578</th>\n",
       "      <td>17</td>\n",
       "      <td>7</td>\n",
       "      <td>0.498134</td>\n",
       "      <td>0.411765</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17834</th>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>0.498237</td>\n",
       "      <td>0.235294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12050</th>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>0.498340</td>\n",
       "      <td>0.235294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4628</th>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>0.498443</td>\n",
       "      <td>0.176471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18056</th>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>0.498546</td>\n",
       "      <td>0.235294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10951</th>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>0.498649</td>\n",
       "      <td>0.176471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16712</th>\n",
       "      <td>17</td>\n",
       "      <td>5</td>\n",
       "      <td>0.498752</td>\n",
       "      <td>0.294118</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6764</th>\n",
       "      <td>17</td>\n",
       "      <td>4</td>\n",
       "      <td>0.498855</td>\n",
       "      <td>0.235294</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16863</th>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>0.498958</td>\n",
       "      <td>0.176471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14394</th>\n",
       "      <td>17</td>\n",
       "      <td>9</td>\n",
       "      <td>0.499061</td>\n",
       "      <td>0.529412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>776</th>\n",
       "      <td>17</td>\n",
       "      <td>3</td>\n",
       "      <td>0.499164</td>\n",
       "      <td>0.176471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14319</th>\n",
       "      <td>17</td>\n",
       "      <td>6</td>\n",
       "      <td>0.499267</td>\n",
       "      <td>0.352941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10856</th>\n",
       "      <td>17</td>\n",
       "      <td>6</td>\n",
       "      <td>0.499370</td>\n",
       "      <td>0.352941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11559</th>\n",
       "      <td>17</td>\n",
       "      <td>6</td>\n",
       "      <td>0.499473</td>\n",
       "      <td>0.352941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3163</th>\n",
       "      <td>17</td>\n",
       "      <td>2</td>\n",
       "      <td>0.499576</td>\n",
       "      <td>0.117647</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19462</th>\n",
       "      <td>17</td>\n",
       "      <td>7</td>\n",
       "      <td>0.499679</td>\n",
       "      <td>0.411765</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23096</th>\n",
       "      <td>17</td>\n",
       "      <td>6</td>\n",
       "      <td>0.499782</td>\n",
       "      <td>0.352941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6693</th>\n",
       "      <td>17</td>\n",
       "      <td>6</td>\n",
       "      <td>0.499885</td>\n",
       "      <td>0.352941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13902</th>\n",
       "      <td>17</td>\n",
       "      <td>6</td>\n",
       "      <td>0.499988</td>\n",
       "      <td>0.352941</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2253 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          sum  max  count_cum  max_rate\n",
       "user_id                                \n",
       "14048    1033   15   0.006260  0.014521\n",
       "7592      897   47   0.011696  0.052397\n",
       "7983      536   23   0.014944  0.042910\n",
       "7931      512   21   0.018046  0.041016\n",
       "19339     368   24   0.020276  0.065217\n",
       "22279     330   63   0.022276  0.190909\n",
       "499       326   21   0.024252  0.064417\n",
       "6569      315   18   0.026160  0.057143\n",
       "3049      300   17   0.027978  0.056667\n",
       "16517     298   18   0.029784  0.060403\n",
       "13167     292   21   0.031554  0.071918\n",
       "12367     263    8   0.033147  0.030418\n",
       "22061     263   13   0.034741  0.049430\n",
       "2664      251   24   0.036262  0.095618\n",
       "20917     221   10   0.037602  0.045249\n",
       "21950     204   14   0.038838  0.068627\n",
       "22348     195   24   0.040019  0.123077\n",
       "2908      187   14   0.041153  0.074866\n",
       "586       183   15   0.042262  0.081967\n",
       "16175     182   29   0.043364  0.159341\n",
       "21739     179   18   0.044449  0.100559\n",
       "313       174   30   0.045504  0.172414\n",
       "3901      174    8   0.046558  0.045977\n",
       "19597     173    6   0.047606  0.034682\n",
       "22492     169   22   0.048630  0.130178\n",
       "17337     164   32   0.049624  0.195122\n",
       "21694     163   11   0.050612  0.067485\n",
       "15162     161   13   0.051588  0.080745\n",
       "8250      160   20   0.052557  0.125000\n",
       "1077      158   12   0.053515  0.075949\n",
       "...       ...  ...        ...       ...\n",
       "15249      17    9   0.497000  0.529412\n",
       "20763      17    9   0.497103  0.529412\n",
       "2999       17    3   0.497206  0.176471\n",
       "2998       17    4   0.497309  0.235294\n",
       "7600       17    5   0.497412  0.294118\n",
       "12816      17    9   0.497515  0.529412\n",
       "82         17    3   0.497618  0.176471\n",
       "2044       17    3   0.497721  0.176471\n",
       "20831      17    4   0.497825  0.235294\n",
       "17988      17    4   0.497928  0.235294\n",
       "13194      17    6   0.498031  0.352941\n",
       "15578      17    7   0.498134  0.411765\n",
       "17834      17    4   0.498237  0.235294\n",
       "12050      17    4   0.498340  0.235294\n",
       "4628       17    3   0.498443  0.176471\n",
       "18056      17    4   0.498546  0.235294\n",
       "10951      17    3   0.498649  0.176471\n",
       "16712      17    5   0.498752  0.294118\n",
       "6764       17    4   0.498855  0.235294\n",
       "16863      17    3   0.498958  0.176471\n",
       "14394      17    9   0.499061  0.529412\n",
       "776        17    3   0.499164  0.176471\n",
       "14319      17    6   0.499267  0.352941\n",
       "10856      17    6   0.499370  0.352941\n",
       "11559      17    6   0.499473  0.352941\n",
       "3163       17    2   0.499576  0.117647\n",
       "19462      17    7   0.499679  0.411765\n",
       "23096      17    6   0.499782  0.352941\n",
       "6693       17    6   0.499885  0.352941\n",
       "13902      17    6   0.499988  0.352941\n",
       "\n",
       "[2253 rows x 4 columns]"
      ]
     },
     "execution_count": 278,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "item_count[:item_count.count_cum.values.searchsorted(0.5)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 283,
   "metadata": {},
   "outputs": [],
   "source": [
    "item_price = uGp['price'].agg(['sum','max']).sort_values(by='sum',ascending=False)\n",
    "item_price['count_cum'] = item_price['sum'].cumsum()/item_price['sum'].sum()\n",
    "item_price['max_rate'] = item_price['max']/item_price['sum']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 282,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum</th>\n",
       "      <th>max</th>\n",
       "      <th>count_cum</th>\n",
       "      <th>max_rate</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>7592</th>\n",
       "      <td>2807.560218</td>\n",
       "      <td>31.490000</td>\n",
       "      <td>0.002872</td>\n",
       "      <td>0.011216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22061</th>\n",
       "      <td>2031.690194</td>\n",
       "      <td>27.770000</td>\n",
       "      <td>0.004950</td>\n",
       "      <td>0.013668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7983</th>\n",
       "      <td>1967.169465</td>\n",
       "      <td>31.490000</td>\n",
       "      <td>0.006962</td>\n",
       "      <td>0.016008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14048</th>\n",
       "      <td>1755.943571</td>\n",
       "      <td>22.770000</td>\n",
       "      <td>0.008759</td>\n",
       "      <td>0.012967</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3049</th>\n",
       "      <td>1629.307662</td>\n",
       "      <td>23.770000</td>\n",
       "      <td>0.010425</td>\n",
       "      <td>0.014589</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499</th>\n",
       "      <td>1481.366972</td>\n",
       "      <td>31.990000</td>\n",
       "      <td>0.011941</td>\n",
       "      <td>0.021595</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19597</th>\n",
       "      <td>1473.090000</td>\n",
       "      <td>22.990000</td>\n",
       "      <td>0.013448</td>\n",
       "      <td>0.015607</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7145</th>\n",
       "      <td>1380.534167</td>\n",
       "      <td>29.770000</td>\n",
       "      <td>0.014860</td>\n",
       "      <td>0.021564</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2484</th>\n",
       "      <td>1118.471333</td>\n",
       "      <td>31.490000</td>\n",
       "      <td>0.016004</td>\n",
       "      <td>0.028154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>710</th>\n",
       "      <td>982.505444</td>\n",
       "      <td>31.490000</td>\n",
       "      <td>0.017009</td>\n",
       "      <td>0.032051</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4459</th>\n",
       "      <td>959.088333</td>\n",
       "      <td>31.990000</td>\n",
       "      <td>0.017990</td>\n",
       "      <td>0.033355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10079</th>\n",
       "      <td>914.010667</td>\n",
       "      <td>27.770000</td>\n",
       "      <td>0.018925</td>\n",
       "      <td>0.030383</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19339</th>\n",
       "      <td>888.274276</td>\n",
       "      <td>30.853333</td>\n",
       "      <td>0.019834</td>\n",
       "      <td>0.034734</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1077</th>\n",
       "      <td>885.080000</td>\n",
       "      <td>32.243333</td>\n",
       "      <td>0.020739</td>\n",
       "      <td>0.036430</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12367</th>\n",
       "      <td>874.296762</td>\n",
       "      <td>24.365000</td>\n",
       "      <td>0.021633</td>\n",
       "      <td>0.027868</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7931</th>\n",
       "      <td>770.529093</td>\n",
       "      <td>18.472857</td>\n",
       "      <td>0.022421</td>\n",
       "      <td>0.023974</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8035</th>\n",
       "      <td>764.742730</td>\n",
       "      <td>29.990000</td>\n",
       "      <td>0.023204</td>\n",
       "      <td>0.039216</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6057</th>\n",
       "      <td>749.110833</td>\n",
       "      <td>32.370000</td>\n",
       "      <td>0.023970</td>\n",
       "      <td>0.043211</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17104</th>\n",
       "      <td>744.763333</td>\n",
       "      <td>31.490000</td>\n",
       "      <td>0.024732</td>\n",
       "      <td>0.042282</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1722</th>\n",
       "      <td>701.413333</td>\n",
       "      <td>29.990000</td>\n",
       "      <td>0.025449</td>\n",
       "      <td>0.042757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13167</th>\n",
       "      <td>688.158495</td>\n",
       "      <td>21.490000</td>\n",
       "      <td>0.026153</td>\n",
       "      <td>0.031228</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1980</th>\n",
       "      <td>673.833333</td>\n",
       "      <td>31.990000</td>\n",
       "      <td>0.026843</td>\n",
       "      <td>0.047475</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12476</th>\n",
       "      <td>673.727500</td>\n",
       "      <td>28.490000</td>\n",
       "      <td>0.027532</td>\n",
       "      <td>0.042287</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20917</th>\n",
       "      <td>666.926381</td>\n",
       "      <td>22.490000</td>\n",
       "      <td>0.028214</td>\n",
       "      <td>0.033722</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22013</th>\n",
       "      <td>664.455952</td>\n",
       "      <td>29.990000</td>\n",
       "      <td>0.028894</td>\n",
       "      <td>0.045135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20873</th>\n",
       "      <td>653.467810</td>\n",
       "      <td>17.490000</td>\n",
       "      <td>0.029562</td>\n",
       "      <td>0.026765</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1760</th>\n",
       "      <td>645.090500</td>\n",
       "      <td>21.490000</td>\n",
       "      <td>0.030222</td>\n",
       "      <td>0.033313</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15562</th>\n",
       "      <td>638.688833</td>\n",
       "      <td>28.300000</td>\n",
       "      <td>0.030875</td>\n",
       "      <td>0.044310</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5868</th>\n",
       "      <td>627.893333</td>\n",
       "      <td>32.490000</td>\n",
       "      <td>0.031518</td>\n",
       "      <td>0.051744</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6569</th>\n",
       "      <td>603.668579</td>\n",
       "      <td>26.927500</td>\n",
       "      <td>0.032135</td>\n",
       "      <td>0.044606</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9593</th>\n",
       "      <td>4.790000</td>\n",
       "      <td>4.790000</td>\n",
       "      <td>0.999876</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6665</th>\n",
       "      <td>4.790000</td>\n",
       "      <td>4.790000</td>\n",
       "      <td>0.999881</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18566</th>\n",
       "      <td>4.790000</td>\n",
       "      <td>4.790000</td>\n",
       "      <td>0.999886</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15617</th>\n",
       "      <td>4.790000</td>\n",
       "      <td>4.790000</td>\n",
       "      <td>0.999891</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22936</th>\n",
       "      <td>4.790000</td>\n",
       "      <td>4.790000</td>\n",
       "      <td>0.999896</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15261</th>\n",
       "      <td>4.790000</td>\n",
       "      <td>4.790000</td>\n",
       "      <td>0.999900</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5453</th>\n",
       "      <td>4.656667</td>\n",
       "      <td>4.656667</td>\n",
       "      <td>0.999905</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16291</th>\n",
       "      <td>4.523333</td>\n",
       "      <td>4.523333</td>\n",
       "      <td>0.999910</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13424</th>\n",
       "      <td>4.390000</td>\n",
       "      <td>4.390000</td>\n",
       "      <td>0.999914</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20466</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999918</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16115</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999922</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10669</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999927</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2344</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999931</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3477</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999935</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11442</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999939</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1770</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999943</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6075</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999947</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14202</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999951</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17312</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999955</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3029</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999959</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12682</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999963</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4077</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999967</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16185</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999971</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7282</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999976</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17597</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999980</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10147</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999984</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12752</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999988</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14306</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999992</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5796</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>0.999996</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5383</th>\n",
       "      <td>3.990000</td>\n",
       "      <td>3.990000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>23082 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                 sum        max  count_cum  max_rate\n",
       "user_id                                             \n",
       "7592     2807.560218  31.490000   0.002872  0.011216\n",
       "22061    2031.690194  27.770000   0.004950  0.013668\n",
       "7983     1967.169465  31.490000   0.006962  0.016008\n",
       "14048    1755.943571  22.770000   0.008759  0.012967\n",
       "3049     1629.307662  23.770000   0.010425  0.014589\n",
       "499      1481.366972  31.990000   0.011941  0.021595\n",
       "19597    1473.090000  22.990000   0.013448  0.015607\n",
       "7145     1380.534167  29.770000   0.014860  0.021564\n",
       "2484     1118.471333  31.490000   0.016004  0.028154\n",
       "710       982.505444  31.490000   0.017009  0.032051\n",
       "4459      959.088333  31.990000   0.017990  0.033355\n",
       "10079     914.010667  27.770000   0.018925  0.030383\n",
       "19339     888.274276  30.853333   0.019834  0.034734\n",
       "1077      885.080000  32.243333   0.020739  0.036430\n",
       "12367     874.296762  24.365000   0.021633  0.027868\n",
       "7931      770.529093  18.472857   0.022421  0.023974\n",
       "8035      764.742730  29.990000   0.023204  0.039216\n",
       "6057      749.110833  32.370000   0.023970  0.043211\n",
       "17104     744.763333  31.490000   0.024732  0.042282\n",
       "1722      701.413333  29.990000   0.025449  0.042757\n",
       "13167     688.158495  21.490000   0.026153  0.031228\n",
       "1980      673.833333  31.990000   0.026843  0.047475\n",
       "12476     673.727500  28.490000   0.027532  0.042287\n",
       "20917     666.926381  22.490000   0.028214  0.033722\n",
       "22013     664.455952  29.990000   0.028894  0.045135\n",
       "20873     653.467810  17.490000   0.029562  0.026765\n",
       "1760      645.090500  21.490000   0.030222  0.033313\n",
       "15562     638.688833  28.300000   0.030875  0.044310\n",
       "5868      627.893333  32.490000   0.031518  0.051744\n",
       "6569      603.668579  26.927500   0.032135  0.044606\n",
       "...              ...        ...        ...       ...\n",
       "9593        4.790000   4.790000   0.999876  1.000000\n",
       "6665        4.790000   4.790000   0.999881  1.000000\n",
       "18566       4.790000   4.790000   0.999886  1.000000\n",
       "15617       4.790000   4.790000   0.999891  1.000000\n",
       "22936       4.790000   4.790000   0.999896  1.000000\n",
       "15261       4.790000   4.790000   0.999900  1.000000\n",
       "5453        4.656667   4.656667   0.999905  1.000000\n",
       "16291       4.523333   4.523333   0.999910  1.000000\n",
       "13424       4.390000   4.390000   0.999914  1.000000\n",
       "20466       3.990000   3.990000   0.999918  1.000000\n",
       "16115       3.990000   3.990000   0.999922  1.000000\n",
       "10669       3.990000   3.990000   0.999927  1.000000\n",
       "2344        3.990000   3.990000   0.999931  1.000000\n",
       "3477        3.990000   3.990000   0.999935  1.000000\n",
       "11442       3.990000   3.990000   0.999939  1.000000\n",
       "1770        3.990000   3.990000   0.999943  1.000000\n",
       "6075        3.990000   3.990000   0.999947  1.000000\n",
       "14202       3.990000   3.990000   0.999951  1.000000\n",
       "17312       3.990000   3.990000   0.999955  1.000000\n",
       "3029        3.990000   3.990000   0.999959  1.000000\n",
       "12682       3.990000   3.990000   0.999963  1.000000\n",
       "4077        3.990000   3.990000   0.999967  1.000000\n",
       "16185       3.990000   3.990000   0.999971  1.000000\n",
       "7282        3.990000   3.990000   0.999976  1.000000\n",
       "17597       3.990000   3.990000   0.999980  1.000000\n",
       "10147       3.990000   3.990000   0.999984  1.000000\n",
       "12752       3.990000   3.990000   0.999988  1.000000\n",
       "14306       3.990000   3.990000   0.999992  1.000000\n",
       "5796        3.990000   3.990000   0.999996  1.000000\n",
       "5383        3.990000   3.990000   1.000000  1.000000\n",
       "\n",
       "[23082 rows x 4 columns]"
      ]
     },
     "execution_count": 282,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "item_price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 287,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>sum</th>\n",
       "      <th>max</th>\n",
       "      <th>min</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>14048</th>\n",
       "      <td>1033</td>\n",
       "      <td>15</td>\n",
       "      <td>1</td>\n",
       "      <td>217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7592</th>\n",
       "      <td>897</td>\n",
       "      <td>47</td>\n",
       "      <td>1</td>\n",
       "      <td>186</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7983</th>\n",
       "      <td>536</td>\n",
       "      <td>23</td>\n",
       "      <td>1</td>\n",
       "      <td>149</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7931</th>\n",
       "      <td>512</td>\n",
       "      <td>21</td>\n",
       "      <td>1</td>\n",
       "      <td>61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19339</th>\n",
       "      <td>368</td>\n",
       "      <td>24</td>\n",
       "      <td>1</td>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22279</th>\n",
       "      <td>330</td>\n",
       "      <td>63</td>\n",
       "      <td>1</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499</th>\n",
       "      <td>326</td>\n",
       "      <td>21</td>\n",
       "      <td>1</td>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6569</th>\n",
       "      <td>315</td>\n",
       "      <td>18</td>\n",
       "      <td>1</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3049</th>\n",
       "      <td>300</td>\n",
       "      <td>17</td>\n",
       "      <td>1</td>\n",
       "      <td>116</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16517</th>\n",
       "      <td>298</td>\n",
       "      <td>18</td>\n",
       "      <td>1</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13167</th>\n",
       "      <td>292</td>\n",
       "      <td>21</td>\n",
       "      <td>1</td>\n",
       "      <td>50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12367</th>\n",
       "      <td>263</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22061</th>\n",
       "      <td>263</td>\n",
       "      <td>13</td>\n",
       "      <td>1</td>\n",
       "      <td>140</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2664</th>\n",
       "      <td>251</td>\n",
       "      <td>24</td>\n",
       "      <td>1</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20917</th>\n",
       "      <td>221</td>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21950</th>\n",
       "      <td>204</td>\n",
       "      <td>14</td>\n",
       "      <td>1</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22348</th>\n",
       "      <td>195</td>\n",
       "      <td>24</td>\n",
       "      <td>1</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2908</th>\n",
       "      <td>187</td>\n",
       "      <td>14</td>\n",
       "      <td>2</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>586</th>\n",
       "      <td>183</td>\n",
       "      <td>15</td>\n",
       "      <td>1</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16175</th>\n",
       "      <td>182</td>\n",
       "      <td>29</td>\n",
       "      <td>1</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21739</th>\n",
       "      <td>179</td>\n",
       "      <td>18</td>\n",
       "      <td>3</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>313</th>\n",
       "      <td>174</td>\n",
       "      <td>30</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3901</th>\n",
       "      <td>174</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "      <td>41</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19597</th>\n",
       "      <td>173</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22492</th>\n",
       "      <td>169</td>\n",
       "      <td>22</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17337</th>\n",
       "      <td>164</td>\n",
       "      <td>32</td>\n",
       "      <td>1</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21694</th>\n",
       "      <td>163</td>\n",
       "      <td>11</td>\n",
       "      <td>2</td>\n",
       "      <td>28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15162</th>\n",
       "      <td>161</td>\n",
       "      <td>13</td>\n",
       "      <td>1</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8250</th>\n",
       "      <td>160</td>\n",
       "      <td>20</td>\n",
       "      <td>1</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1077</th>\n",
       "      <td>158</td>\n",
       "      <td>12</td>\n",
       "      <td>1</td>\n",
       "      <td>48</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8997</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8996</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8941</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18037</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8942</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18067</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18066</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8947</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18064</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8949</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8951</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18060</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18059</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8952</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8959</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8961</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8962</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8963</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18052</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18051</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8967</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8968</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18047</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18044</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8983</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8987</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18040</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18039</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8991</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>23082 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          sum  max  min  count\n",
       "user_id                       \n",
       "14048    1033   15    1    217\n",
       "7592      897   47    1    186\n",
       "7983      536   23    1    149\n",
       "7931      512   21    1     61\n",
       "19339     368   24    1     53\n",
       "22279     330   63    1     13\n",
       "499       326   21    1    108\n",
       "6569      315   18    1     40\n",
       "3049      300   17    1    116\n",
       "16517     298   18    1     34\n",
       "13167     292   21    1     50\n",
       "12367     263    8    1     60\n",
       "22061     263   13    1    140\n",
       "2664      251   24    1     29\n",
       "20917     221   10    1     48\n",
       "21950     204   14    1     27\n",
       "22348     195   24    1     30\n",
       "2908      187   14    2     31\n",
       "586       183   15    1     32\n",
       "16175     182   29    1     13\n",
       "21739     179   18    3     21\n",
       "313       174   30    2     20\n",
       "3901      174    8    1     41\n",
       "19597     173    6    1    108\n",
       "22492     169   22    1     19\n",
       "17337     164   32    1     11\n",
       "21694     163   11    2     28\n",
       "15162     161   13    1     26\n",
       "8250      160   20    1     34\n",
       "1077      158   12    1     48\n",
       "...       ...  ...  ...    ...\n",
       "8997        1    1    1      1\n",
       "8996        1    1    1      1\n",
       "8941        1    1    1      1\n",
       "18037       1    1    1      1\n",
       "8942        1    1    1      1\n",
       "18067       1    1    1      1\n",
       "18066       1    1    1      1\n",
       "8947        1    1    1      1\n",
       "18064       1    1    1      1\n",
       "8949        1    1    1      1\n",
       "8951        1    1    1      1\n",
       "18060       1    1    1      1\n",
       "18059       1    1    1      1\n",
       "8952        1    1    1      1\n",
       "8959        1    1    1      1\n",
       "8961        1    1    1      1\n",
       "8962        1    1    1      1\n",
       "8963        1    1    1      1\n",
       "18052       1    1    1      1\n",
       "18051       1    1    1      1\n",
       "8967        1    1    1      1\n",
       "8968        1    1    1      1\n",
       "18047       1    1    1      1\n",
       "18044       1    1    1      1\n",
       "8983        1    1    1      1\n",
       "8987        1    1    1      1\n",
       "18040       1    1    1      1\n",
       "18039       1    1    1      1\n",
       "8991        1    1    1      1\n",
       "1           1    1    1      1\n",
       "\n",
       "[23082 rows x 4 columns]"
      ]
     },
     "execution_count": 287,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "uGp['count'].agg(['sum','max','min','count']).sort_values(by='sum',ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 318,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.reset_index(inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 319,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>buy_date</th>\n",
       "      <th>user_id</th>\n",
       "      <th>count</th>\n",
       "      <th>price</th>\n",
       "      <th>single_price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>11.77</td>\n",
       "      <td>11.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>12.00</td>\n",
       "      <td>12.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>77.00</td>\n",
       "      <td>15.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>1997-01-02</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "      <td>10.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>1997-03-30</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "      <td>10.38</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   index   buy_date  user_id  count  price  single_price\n",
       "0      0 1997-01-01        1      1  11.77         11.77\n",
       "1      1 1997-01-12        2      1  12.00         12.00\n",
       "2      2 1997-01-12        2      5  77.00         15.40\n",
       "3      3 1997-01-02        3      2  20.76         10.38\n",
       "4      4 1997-03-30        3      2  20.76         10.38"
      ]
     },
     "execution_count": 319,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 335,
   "metadata": {},
   "outputs": [],
   "source": [
    "def mean_interval(gb):\n",
    "    d = gb.sort_values()\n",
    "    return (d - d.shift(1)).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 336,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_ts = uGp['buy_date'].agg(['min','max',mean_interval])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 337,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "      <th>mean_interval</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>0 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1997-01-02</td>\n",
       "      <td>1998-05-28</td>\n",
       "      <td>102 days 04:48:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-12-12</td>\n",
       "      <td>115 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-01-03</td>\n",
       "      <td>36 days 16:48:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-03-22</td>\n",
       "      <td>222 days 12:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-03-29</td>\n",
       "      <td>64 days 13:42:51.428571</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-06-08</td>\n",
       "      <td>261 days 12:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1997-01-21</td>\n",
       "      <td>1997-01-21</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-02-20</td>\n",
       "      <td>138 days 08:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-09-10</td>\n",
       "      <td>84 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>1997-01-04</td>\n",
       "      <td>1997-01-04</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-06-10</td>\n",
       "      <td>160 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-18</td>\n",
       "      <td>17 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-13</td>\n",
       "      <td>12 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-01-20</td>\n",
       "      <td>384 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-06-08</td>\n",
       "      <td>74 days 17:08:34.285714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-26</td>\n",
       "      <td>25 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>1997-01-07</td>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>5 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-03-08</td>\n",
       "      <td>33 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-04-26</td>\n",
       "      <td>43 days 15:16:21.818181</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-02-14</td>\n",
       "      <td>44 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23541</th>\n",
       "      <td>1997-04-02</td>\n",
       "      <td>1997-04-02</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23542</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23543</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23544</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1998-01-24</td>\n",
       "      <td>152 days 12:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23545</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23546</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23547</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-04-07</td>\n",
       "      <td>13 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23548</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23549</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23550</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23551</th>\n",
       "      <td>1997-06-19</td>\n",
       "      <td>1997-09-11</td>\n",
       "      <td>21 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23552</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-04-03</td>\n",
       "      <td>9 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23553</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-28</td>\n",
       "      <td>3 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23554</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1998-02-01</td>\n",
       "      <td>313 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23555</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1998-06-10</td>\n",
       "      <td>110 days 12:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23556</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1998-06-07</td>\n",
       "      <td>73 days 04:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23557</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23558</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1998-02-25</td>\n",
       "      <td>112 days 08:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23559</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-06-27</td>\n",
       "      <td>47 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23560</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23561</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1998-05-29</td>\n",
       "      <td>215 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23562</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23563</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-10-04</td>\n",
       "      <td>193 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23564</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-11-30</td>\n",
       "      <td>125 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23565</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23566</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23567</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23568</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-04-22</td>\n",
       "      <td>14 days 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23569</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23570</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-26</td>\n",
       "      <td>1 days 00:00:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>23082 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               min        max           mean_interval\n",
       "user_id                                              \n",
       "1       1997-01-01 1997-01-01                     NaT\n",
       "2       1997-01-12 1997-01-12         0 days 00:00:00\n",
       "3       1997-01-02 1998-05-28       102 days 04:48:00\n",
       "4       1997-01-01 1997-12-12       115 days 00:00:00\n",
       "5       1997-01-01 1998-01-03        36 days 16:48:00\n",
       "6       1997-01-01 1997-01-01                     NaT\n",
       "7       1997-01-01 1998-03-22       222 days 12:00:00\n",
       "8       1997-01-01 1998-03-29 64 days 13:42:51.428571\n",
       "9       1997-01-01 1998-06-08       261 days 12:00:00\n",
       "10      1997-01-21 1997-01-21                     NaT\n",
       "11      1997-01-01 1998-02-20       138 days 08:00:00\n",
       "12      1997-01-01 1997-01-01                     NaT\n",
       "13      1997-01-01 1997-01-01                     NaT\n",
       "14      1997-01-01 1997-01-01                     NaT\n",
       "15      1997-01-01 1997-01-01                     NaT\n",
       "16      1997-01-01 1997-09-10        84 days 00:00:00\n",
       "17      1997-01-01 1997-01-01                     NaT\n",
       "18      1997-01-04 1997-01-04                     NaT\n",
       "19      1997-01-01 1997-06-10       160 days 00:00:00\n",
       "20      1997-01-01 1997-01-18        17 days 00:00:00\n",
       "21      1997-01-01 1997-01-13        12 days 00:00:00\n",
       "22      1997-01-01 1997-01-01                     NaT\n",
       "23      1997-01-01 1997-01-01                     NaT\n",
       "24      1997-01-01 1998-01-20       384 days 00:00:00\n",
       "25      1997-01-01 1998-06-08 74 days 17:08:34.285714\n",
       "26      1997-01-01 1997-01-26        25 days 00:00:00\n",
       "27      1997-01-07 1997-01-12         5 days 00:00:00\n",
       "28      1997-01-01 1997-03-08        33 days 00:00:00\n",
       "29      1997-01-01 1998-04-26 43 days 15:16:21.818181\n",
       "30      1997-01-01 1997-02-14        44 days 00:00:00\n",
       "...            ...        ...                     ...\n",
       "23541   1997-04-02 1997-04-02                     NaT\n",
       "23542   1997-03-25 1997-03-25                     NaT\n",
       "23543   1997-03-25 1997-03-25                     NaT\n",
       "23544   1997-03-25 1998-01-24       152 days 12:00:00\n",
       "23545   1997-03-25 1997-03-25                     NaT\n",
       "23546   1997-03-25 1997-03-25                     NaT\n",
       "23547   1997-03-25 1997-04-07        13 days 00:00:00\n",
       "23548   1997-03-25 1997-03-25                     NaT\n",
       "23549   1997-03-25 1997-03-25                     NaT\n",
       "23550   1997-03-25 1997-03-25                     NaT\n",
       "23551   1997-06-19 1997-09-11        21 days 00:00:00\n",
       "23552   1997-03-25 1997-04-03         9 days 00:00:00\n",
       "23553   1997-03-25 1997-03-28         3 days 00:00:00\n",
       "23554   1997-03-25 1998-02-01       313 days 00:00:00\n",
       "23555   1997-03-25 1998-06-10       110 days 12:00:00\n",
       "23556   1997-03-25 1998-06-07        73 days 04:00:00\n",
       "23557   1997-03-25 1997-03-25                     NaT\n",
       "23558   1997-03-25 1998-02-25       112 days 08:00:00\n",
       "23559   1997-03-25 1997-06-27        47 days 00:00:00\n",
       "23560   1997-03-25 1997-03-25                     NaT\n",
       "23561   1997-03-25 1998-05-29       215 days 00:00:00\n",
       "23562   1997-03-25 1997-03-25                     NaT\n",
       "23563   1997-03-25 1997-10-04       193 days 00:00:00\n",
       "23564   1997-03-25 1997-11-30       125 days 00:00:00\n",
       "23565   1997-03-25 1997-03-25                     NaT\n",
       "23566   1997-03-25 1997-03-25                     NaT\n",
       "23567   1997-03-25 1997-03-25                     NaT\n",
       "23568   1997-03-25 1997-04-22        14 days 00:00:00\n",
       "23569   1997-03-25 1997-03-25                     NaT\n",
       "23570   1997-03-25 1997-03-26         1 days 00:00:00\n",
       "\n",
       "[23082 rows x 3 columns]"
      ]
     },
     "execution_count": 337,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 338,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime,timedelta\n",
    "user_ts['interval'] = (datetime.now() - user_ts['max']).apply(lambda x:x.days)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 339,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>min</th>\n",
       "      <th>max</th>\n",
       "      <th>mean_interval</th>\n",
       "      <th>interval</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>user_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>0 days 00:00:00</td>\n",
       "      <td>8101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1997-01-02</td>\n",
       "      <td>1998-05-28</td>\n",
       "      <td>102 days 04:48:00</td>\n",
       "      <td>7600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-12-12</td>\n",
       "      <td>115 days 00:00:00</td>\n",
       "      <td>7767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-01-03</td>\n",
       "      <td>36 days 16:48:00</td>\n",
       "      <td>7745</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-03-22</td>\n",
       "      <td>222 days 12:00:00</td>\n",
       "      <td>7667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-03-29</td>\n",
       "      <td>64 days 13:42:51.428571</td>\n",
       "      <td>7660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-06-08</td>\n",
       "      <td>261 days 12:00:00</td>\n",
       "      <td>7589</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1997-01-21</td>\n",
       "      <td>1997-01-21</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-02-20</td>\n",
       "      <td>138 days 08:00:00</td>\n",
       "      <td>7697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-09-10</td>\n",
       "      <td>84 days 00:00:00</td>\n",
       "      <td>7860</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>1997-01-04</td>\n",
       "      <td>1997-01-04</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-06-10</td>\n",
       "      <td>160 days 00:00:00</td>\n",
       "      <td>7952</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-18</td>\n",
       "      <td>17 days 00:00:00</td>\n",
       "      <td>8095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-13</td>\n",
       "      <td>12 days 00:00:00</td>\n",
       "      <td>8100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-01-20</td>\n",
       "      <td>384 days 00:00:00</td>\n",
       "      <td>7728</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-06-08</td>\n",
       "      <td>74 days 17:08:34.285714</td>\n",
       "      <td>7589</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-01-26</td>\n",
       "      <td>25 days 00:00:00</td>\n",
       "      <td>8087</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>1997-01-07</td>\n",
       "      <td>1997-01-12</td>\n",
       "      <td>5 days 00:00:00</td>\n",
       "      <td>8101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-03-08</td>\n",
       "      <td>33 days 00:00:00</td>\n",
       "      <td>8046</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1998-04-26</td>\n",
       "      <td>43 days 15:16:21.818181</td>\n",
       "      <td>7632</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>1997-01-01</td>\n",
       "      <td>1997-02-14</td>\n",
       "      <td>44 days 00:00:00</td>\n",
       "      <td>8068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23541</th>\n",
       "      <td>1997-04-02</td>\n",
       "      <td>1997-04-02</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8021</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23542</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23543</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23544</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1998-01-24</td>\n",
       "      <td>152 days 12:00:00</td>\n",
       "      <td>7724</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23545</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23546</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23547</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-04-07</td>\n",
       "      <td>13 days 00:00:00</td>\n",
       "      <td>8016</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23548</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23549</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23550</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23551</th>\n",
       "      <td>1997-06-19</td>\n",
       "      <td>1997-09-11</td>\n",
       "      <td>21 days 00:00:00</td>\n",
       "      <td>7859</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23552</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-04-03</td>\n",
       "      <td>9 days 00:00:00</td>\n",
       "      <td>8020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23553</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-28</td>\n",
       "      <td>3 days 00:00:00</td>\n",
       "      <td>8026</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23554</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1998-02-01</td>\n",
       "      <td>313 days 00:00:00</td>\n",
       "      <td>7716</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23555</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1998-06-10</td>\n",
       "      <td>110 days 12:00:00</td>\n",
       "      <td>7587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23556</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1998-06-07</td>\n",
       "      <td>73 days 04:00:00</td>\n",
       "      <td>7590</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23557</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23558</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1998-02-25</td>\n",
       "      <td>112 days 08:00:00</td>\n",
       "      <td>7692</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23559</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-06-27</td>\n",
       "      <td>47 days 00:00:00</td>\n",
       "      <td>7935</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23560</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23561</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1998-05-29</td>\n",
       "      <td>215 days 00:00:00</td>\n",
       "      <td>7599</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23562</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23563</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-10-04</td>\n",
       "      <td>193 days 00:00:00</td>\n",
       "      <td>7836</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23564</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-11-30</td>\n",
       "      <td>125 days 00:00:00</td>\n",
       "      <td>7779</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23565</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23566</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23567</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23568</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-04-22</td>\n",
       "      <td>14 days 00:00:00</td>\n",
       "      <td>8001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23569</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>NaT</td>\n",
       "      <td>8029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23570</th>\n",
       "      <td>1997-03-25</td>\n",
       "      <td>1997-03-26</td>\n",
       "      <td>1 days 00:00:00</td>\n",
       "      <td>8028</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>23082 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               min        max           mean_interval  interval\n",
       "user_id                                                        \n",
       "1       1997-01-01 1997-01-01                     NaT      8112\n",
       "2       1997-01-12 1997-01-12         0 days 00:00:00      8101\n",
       "3       1997-01-02 1998-05-28       102 days 04:48:00      7600\n",
       "4       1997-01-01 1997-12-12       115 days 00:00:00      7767\n",
       "5       1997-01-01 1998-01-03        36 days 16:48:00      7745\n",
       "6       1997-01-01 1997-01-01                     NaT      8112\n",
       "7       1997-01-01 1998-03-22       222 days 12:00:00      7667\n",
       "8       1997-01-01 1998-03-29 64 days 13:42:51.428571      7660\n",
       "9       1997-01-01 1998-06-08       261 days 12:00:00      7589\n",
       "10      1997-01-21 1997-01-21                     NaT      8092\n",
       "11      1997-01-01 1998-02-20       138 days 08:00:00      7697\n",
       "12      1997-01-01 1997-01-01                     NaT      8112\n",
       "13      1997-01-01 1997-01-01                     NaT      8112\n",
       "14      1997-01-01 1997-01-01                     NaT      8112\n",
       "15      1997-01-01 1997-01-01                     NaT      8112\n",
       "16      1997-01-01 1997-09-10        84 days 00:00:00      7860\n",
       "17      1997-01-01 1997-01-01                     NaT      8112\n",
       "18      1997-01-04 1997-01-04                     NaT      8109\n",
       "19      1997-01-01 1997-06-10       160 days 00:00:00      7952\n",
       "20      1997-01-01 1997-01-18        17 days 00:00:00      8095\n",
       "21      1997-01-01 1997-01-13        12 days 00:00:00      8100\n",
       "22      1997-01-01 1997-01-01                     NaT      8112\n",
       "23      1997-01-01 1997-01-01                     NaT      8112\n",
       "24      1997-01-01 1998-01-20       384 days 00:00:00      7728\n",
       "25      1997-01-01 1998-06-08 74 days 17:08:34.285714      7589\n",
       "26      1997-01-01 1997-01-26        25 days 00:00:00      8087\n",
       "27      1997-01-07 1997-01-12         5 days 00:00:00      8101\n",
       "28      1997-01-01 1997-03-08        33 days 00:00:00      8046\n",
       "29      1997-01-01 1998-04-26 43 days 15:16:21.818181      7632\n",
       "30      1997-01-01 1997-02-14        44 days 00:00:00      8068\n",
       "...            ...        ...                     ...       ...\n",
       "23541   1997-04-02 1997-04-02                     NaT      8021\n",
       "23542   1997-03-25 1997-03-25                     NaT      8029\n",
       "23543   1997-03-25 1997-03-25                     NaT      8029\n",
       "23544   1997-03-25 1998-01-24       152 days 12:00:00      7724\n",
       "23545   1997-03-25 1997-03-25                     NaT      8029\n",
       "23546   1997-03-25 1997-03-25                     NaT      8029\n",
       "23547   1997-03-25 1997-04-07        13 days 00:00:00      8016\n",
       "23548   1997-03-25 1997-03-25                     NaT      8029\n",
       "23549   1997-03-25 1997-03-25                     NaT      8029\n",
       "23550   1997-03-25 1997-03-25                     NaT      8029\n",
       "23551   1997-06-19 1997-09-11        21 days 00:00:00      7859\n",
       "23552   1997-03-25 1997-04-03         9 days 00:00:00      8020\n",
       "23553   1997-03-25 1997-03-28         3 days 00:00:00      8026\n",
       "23554   1997-03-25 1998-02-01       313 days 00:00:00      7716\n",
       "23555   1997-03-25 1998-06-10       110 days 12:00:00      7587\n",
       "23556   1997-03-25 1998-06-07        73 days 04:00:00      7590\n",
       "23557   1997-03-25 1997-03-25                     NaT      8029\n",
       "23558   1997-03-25 1998-02-25       112 days 08:00:00      7692\n",
       "23559   1997-03-25 1997-06-27        47 days 00:00:00      7935\n",
       "23560   1997-03-25 1997-03-25                     NaT      8029\n",
       "23561   1997-03-25 1998-05-29       215 days 00:00:00      7599\n",
       "23562   1997-03-25 1997-03-25                     NaT      8029\n",
       "23563   1997-03-25 1997-10-04       193 days 00:00:00      7836\n",
       "23564   1997-03-25 1997-11-30       125 days 00:00:00      7779\n",
       "23565   1997-03-25 1997-03-25                     NaT      8029\n",
       "23566   1997-03-25 1997-03-25                     NaT      8029\n",
       "23567   1997-03-25 1997-03-25                     NaT      8029\n",
       "23568   1997-03-25 1997-04-22        14 days 00:00:00      8001\n",
       "23569   1997-03-25 1997-03-25                     NaT      8029\n",
       "23570   1997-03-25 1997-03-26         1 days 00:00:00      8028\n",
       "\n",
       "[23082 rows x 4 columns]"
      ]
     },
     "execution_count": 339,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 346,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rebuy(t):\n",
    "    x= t.sort_values()[:2]\n",
    "    return x.values[1]-x.values[0] if len(x)==2 else None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 347,
   "metadata": {},
   "outputs": [],
   "source": [
    "first_rebuy = uGp['buy_date'].agg(rebuy)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 348,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "user_id\n",
       "1            NaT\n",
       "2         0 days\n",
       "3        87 days\n",
       "4        17 days\n",
       "5        13 days\n",
       "6            NaT\n",
       "7       283 days\n",
       "8        43 days\n",
       "9       132 days\n",
       "10           NaT\n",
       "11       27 days\n",
       "12           NaT\n",
       "13           NaT\n",
       "14           NaT\n",
       "15           NaT\n",
       "16      193 days\n",
       "17           NaT\n",
       "18           NaT\n",
       "19      160 days\n",
       "20       17 days\n",
       "21       12 days\n",
       "22           NaT\n",
       "23           NaT\n",
       "24      384 days\n",
       "25      198 days\n",
       "26       25 days\n",
       "27        5 days\n",
       "28       26 days\n",
       "29       36 days\n",
       "30       44 days\n",
       "          ...   \n",
       "23541        NaT\n",
       "23542        NaT\n",
       "23543        NaT\n",
       "23544    51 days\n",
       "23545        NaT\n",
       "23546        NaT\n",
       "23547    13 days\n",
       "23548        NaT\n",
       "23549        NaT\n",
       "23550        NaT\n",
       "23551    50 days\n",
       "23552     9 days\n",
       "23553     3 days\n",
       "23554   313 days\n",
       "23555   214 days\n",
       "23556    77 days\n",
       "23557        NaT\n",
       "23558    54 days\n",
       "23559    54 days\n",
       "23560        NaT\n",
       "23561   309 days\n",
       "23562        NaT\n",
       "23563   193 days\n",
       "23564    57 days\n",
       "23565        NaT\n",
       "23566        NaT\n",
       "23567        NaT\n",
       "23568    11 days\n",
       "23569        NaT\n",
       "23570     1 days\n",
       "Name: buy_date, Length: 23082, dtype: timedelta64[ns]"
      ]
     },
     "execution_count": 348,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first_rebuy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 349,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count                       11455\n",
       "mean     109 days 04:59:03.745089\n",
       "std      122 days 10:28:05.162491\n",
       "min               0 days 00:00:00\n",
       "25%              16 days 00:00:00\n",
       "50%              55 days 00:00:00\n",
       "75%             169 days 00:00:00\n",
       "max             533 days 00:00:00\n",
       "Name: buy_date, dtype: object"
      ]
     },
     "execution_count": 349,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first_rebuy.describe()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 351,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 67663 entries, 0 to 67662\n",
      "Data columns (total 6 columns):\n",
      "index           67663 non-null int64\n",
      "buy_date        67663 non-null datetime64[ns]\n",
      "user_id         67663 non-null int64\n",
      "count           67663 non-null int64\n",
      "price           67663 non-null float64\n",
      "single_price    67663 non-null float64\n",
      "dtypes: datetime64[ns](1), float64(2), int64(3)\n",
      "memory usage: 3.1 MB\n"
     ]
    }
   ],
   "source": [
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 352,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.set_index('buy_date',inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 357,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PeriodIndex(['1997-01', '1997-01', '1997-01', '1997-01', '1997-03', '1997-04',\n",
       "             '1997-11', '1997-11', '1998-05', '1997-01',\n",
       "             ...\n",
       "             '1997-11', '1997-03', '1997-03', '1997-03', '1997-03', '1997-04',\n",
       "             '1997-04', '1997-03', '1997-03', '1997-03'],\n",
       "            dtype='period[M]', name='buy_date', length=67663, freq='M')"
      ]
     },
     "execution_count": 357,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.index.to_period('M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 360,
   "metadata": {},
   "outputs": [],
   "source": [
    "mdata = data.to_period('M')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 367,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rebuy_rate(m):\n",
    "    t = m.value_counts()\n",
    "    return t[t>1].shape[0]/t.shape[0]\n",
    "rebuy_rates = mdata.groupby(['buy_date'])['user_id'].agg(rebuy_rate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 371,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "/* Put everything inside the global mpl namespace */\n",
       "window.mpl = {};\n",
       "\n",
       "\n",
       "mpl.get_websocket_type = function() {\n",
       "    if (typeof(WebSocket) !== 'undefined') {\n",
       "        return WebSocket;\n",
       "    } else if (typeof(MozWebSocket) !== 'undefined') {\n",
       "        return MozWebSocket;\n",
       "    } else {\n",
       "        alert('Your browser does not have WebSocket support.' +\n",
       "              'Please try Chrome, Safari or Firefox ≥ 6. ' +\n",
       "              'Firefox 4 and 5 are also supported but you ' +\n",
       "              'have to enable WebSockets in about:config.');\n",
       "    };\n",
       "}\n",
       "\n",
       "mpl.figure = function(figure_id, websocket, ondownload, parent_element) {\n",
       "    this.id = figure_id;\n",
       "\n",
       "    this.ws = websocket;\n",
       "\n",
       "    this.supports_binary = (this.ws.binaryType != undefined);\n",
       "\n",
       "    if (!this.supports_binary) {\n",
       "        var warnings = document.getElementById(\"mpl-warnings\");\n",
       "        if (warnings) {\n",
       "            warnings.style.display = 'block';\n",
       "            warnings.textContent = (\n",
       "                \"This browser does not support binary websocket messages. \" +\n",
       "                    \"Performance may be slow.\");\n",
       "        }\n",
       "    }\n",
       "\n",
       "    this.imageObj = new Image();\n",
       "\n",
       "    this.context = undefined;\n",
       "    this.message = undefined;\n",
       "    this.canvas = undefined;\n",
       "    this.rubberband_canvas = undefined;\n",
       "    this.rubberband_context = undefined;\n",
       "    this.format_dropdown = undefined;\n",
       "\n",
       "    this.image_mode = 'full';\n",
       "\n",
       "    this.root = $('<div/>');\n",
       "    this._root_extra_style(this.root)\n",
       "    this.root.attr('style', 'display: inline-block');\n",
       "\n",
       "    $(parent_element).append(this.root);\n",
       "\n",
       "    this._init_header(this);\n",
       "    this._init_canvas(this);\n",
       "    this._init_toolbar(this);\n",
       "\n",
       "    var fig = this;\n",
       "\n",
       "    this.waiting = false;\n",
       "\n",
       "    this.ws.onopen =  function () {\n",
       "            fig.send_message(\"supports_binary\", {value: fig.supports_binary});\n",
       "            fig.send_message(\"send_image_mode\", {});\n",
       "            if (mpl.ratio != 1) {\n",
       "                fig.send_message(\"set_dpi_ratio\", {'dpi_ratio': mpl.ratio});\n",
       "            }\n",
       "            fig.send_message(\"refresh\", {});\n",
       "        }\n",
       "\n",
       "    this.imageObj.onload = function() {\n",
       "            if (fig.image_mode == 'full') {\n",
       "                // Full images could contain transparency (where diff images\n",
       "                // almost always do), so we need to clear the canvas so that\n",
       "                // there is no ghosting.\n",
       "                fig.context.clearRect(0, 0, fig.canvas.width, fig.canvas.height);\n",
       "            }\n",
       "            fig.context.drawImage(fig.imageObj, 0, 0);\n",
       "        };\n",
       "\n",
       "    this.imageObj.onunload = function() {\n",
       "        fig.ws.close();\n",
       "    }\n",
       "\n",
       "    this.ws.onmessage = this._make_on_message_function(this);\n",
       "\n",
       "    this.ondownload = ondownload;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_header = function() {\n",
       "    var titlebar = $(\n",
       "        '<div class=\"ui-dialog-titlebar ui-widget-header ui-corner-all ' +\n",
       "        'ui-helper-clearfix\"/>');\n",
       "    var titletext = $(\n",
       "        '<div class=\"ui-dialog-title\" style=\"width: 100%; ' +\n",
       "        'text-align: center; padding: 3px;\"/>');\n",
       "    titlebar.append(titletext)\n",
       "    this.root.append(titlebar);\n",
       "    this.header = titletext[0];\n",
       "}\n",
       "\n",
       "\n",
       "\n",
       "mpl.figure.prototype._canvas_extra_style = function(canvas_div) {\n",
       "\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype._root_extra_style = function(canvas_div) {\n",
       "\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_canvas = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var canvas_div = $('<div/>');\n",
       "\n",
       "    canvas_div.attr('style', 'position: relative; clear: both; outline: 0');\n",
       "\n",
       "    function canvas_keyboard_event(event) {\n",
       "        return fig.key_event(event, event['data']);\n",
       "    }\n",
       "\n",
       "    canvas_div.keydown('key_press', canvas_keyboard_event);\n",
       "    canvas_div.keyup('key_release', canvas_keyboard_event);\n",
       "    this.canvas_div = canvas_div\n",
       "    this._canvas_extra_style(canvas_div)\n",
       "    this.root.append(canvas_div);\n",
       "\n",
       "    var canvas = $('<canvas/>');\n",
       "    canvas.addClass('mpl-canvas');\n",
       "    canvas.attr('style', \"left: 0; top: 0; z-index: 0; outline: 0\")\n",
       "\n",
       "    this.canvas = canvas[0];\n",
       "    this.context = canvas[0].getContext(\"2d\");\n",
       "\n",
       "    var backingStore = this.context.backingStorePixelRatio ||\n",
       "\tthis.context.webkitBackingStorePixelRatio ||\n",
       "\tthis.context.mozBackingStorePixelRatio ||\n",
       "\tthis.context.msBackingStorePixelRatio ||\n",
       "\tthis.context.oBackingStorePixelRatio ||\n",
       "\tthis.context.backingStorePixelRatio || 1;\n",
       "\n",
       "    mpl.ratio = (window.devicePixelRatio || 1) / backingStore;\n",
       "\n",
       "    var rubberband = $('<canvas/>');\n",
       "    rubberband.attr('style', \"position: absolute; left: 0; top: 0; z-index: 1;\")\n",
       "\n",
       "    var pass_mouse_events = true;\n",
       "\n",
       "    canvas_div.resizable({\n",
       "        start: function(event, ui) {\n",
       "            pass_mouse_events = false;\n",
       "        },\n",
       "        resize: function(event, ui) {\n",
       "            fig.request_resize(ui.size.width, ui.size.height);\n",
       "        },\n",
       "        stop: function(event, ui) {\n",
       "            pass_mouse_events = true;\n",
       "            fig.request_resize(ui.size.width, ui.size.height);\n",
       "        },\n",
       "    });\n",
       "\n",
       "    function mouse_event_fn(event) {\n",
       "        if (pass_mouse_events)\n",
       "            return fig.mouse_event(event, event['data']);\n",
       "    }\n",
       "\n",
       "    rubberband.mousedown('button_press', mouse_event_fn);\n",
       "    rubberband.mouseup('button_release', mouse_event_fn);\n",
       "    // Throttle sequential mouse events to 1 every 20ms.\n",
       "    rubberband.mousemove('motion_notify', mouse_event_fn);\n",
       "\n",
       "    rubberband.mouseenter('figure_enter', mouse_event_fn);\n",
       "    rubberband.mouseleave('figure_leave', mouse_event_fn);\n",
       "\n",
       "    canvas_div.on(\"wheel\", function (event) {\n",
       "        event = event.originalEvent;\n",
       "        event['data'] = 'scroll'\n",
       "        if (event.deltaY < 0) {\n",
       "            event.step = 1;\n",
       "        } else {\n",
       "            event.step = -1;\n",
       "        }\n",
       "        mouse_event_fn(event);\n",
       "    });\n",
       "\n",
       "    canvas_div.append(canvas);\n",
       "    canvas_div.append(rubberband);\n",
       "\n",
       "    this.rubberband = rubberband;\n",
       "    this.rubberband_canvas = rubberband[0];\n",
       "    this.rubberband_context = rubberband[0].getContext(\"2d\");\n",
       "    this.rubberband_context.strokeStyle = \"#000000\";\n",
       "\n",
       "    this._resize_canvas = function(width, height) {\n",
       "        // Keep the size of the canvas, canvas container, and rubber band\n",
       "        // canvas in synch.\n",
       "        canvas_div.css('width', width)\n",
       "        canvas_div.css('height', height)\n",
       "\n",
       "        canvas.attr('width', width * mpl.ratio);\n",
       "        canvas.attr('height', height * mpl.ratio);\n",
       "        canvas.attr('style', 'width: ' + width + 'px; height: ' + height + 'px;');\n",
       "\n",
       "        rubberband.attr('width', width);\n",
       "        rubberband.attr('height', height);\n",
       "    }\n",
       "\n",
       "    // Set the figure to an initial 600x600px, this will subsequently be updated\n",
       "    // upon first draw.\n",
       "    this._resize_canvas(600, 600);\n",
       "\n",
       "    // Disable right mouse context menu.\n",
       "    $(this.rubberband_canvas).bind(\"contextmenu\",function(e){\n",
       "        return false;\n",
       "    });\n",
       "\n",
       "    function set_focus () {\n",
       "        canvas.focus();\n",
       "        canvas_div.focus();\n",
       "    }\n",
       "\n",
       "    window.setTimeout(set_focus, 100);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_toolbar = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var nav_element = $('<div/>')\n",
       "    nav_element.attr('style', 'width: 100%');\n",
       "    this.root.append(nav_element);\n",
       "\n",
       "    // Define a callback function for later on.\n",
       "    function toolbar_event(event) {\n",
       "        return fig.toolbar_button_onclick(event['data']);\n",
       "    }\n",
       "    function toolbar_mouse_event(event) {\n",
       "        return fig.toolbar_button_onmouseover(event['data']);\n",
       "    }\n",
       "\n",
       "    for(var toolbar_ind in mpl.toolbar_items) {\n",
       "        var name = mpl.toolbar_items[toolbar_ind][0];\n",
       "        var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
       "        var image = mpl.toolbar_items[toolbar_ind][2];\n",
       "        var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
       "\n",
       "        if (!name) {\n",
       "            // put a spacer in here.\n",
       "            continue;\n",
       "        }\n",
       "        var button = $('<button/>');\n",
       "        button.addClass('ui-button ui-widget ui-state-default ui-corner-all ' +\n",
       "                        'ui-button-icon-only');\n",
       "        button.attr('role', 'button');\n",
       "        button.attr('aria-disabled', 'false');\n",
       "        button.click(method_name, toolbar_event);\n",
       "        button.mouseover(tooltip, toolbar_mouse_event);\n",
       "\n",
       "        var icon_img = $('<span/>');\n",
       "        icon_img.addClass('ui-button-icon-primary ui-icon');\n",
       "        icon_img.addClass(image);\n",
       "        icon_img.addClass('ui-corner-all');\n",
       "\n",
       "        var tooltip_span = $('<span/>');\n",
       "        tooltip_span.addClass('ui-button-text');\n",
       "        tooltip_span.html(tooltip);\n",
       "\n",
       "        button.append(icon_img);\n",
       "        button.append(tooltip_span);\n",
       "\n",
       "        nav_element.append(button);\n",
       "    }\n",
       "\n",
       "    var fmt_picker_span = $('<span/>');\n",
       "\n",
       "    var fmt_picker = $('<select/>');\n",
       "    fmt_picker.addClass('mpl-toolbar-option ui-widget ui-widget-content');\n",
       "    fmt_picker_span.append(fmt_picker);\n",
       "    nav_element.append(fmt_picker_span);\n",
       "    this.format_dropdown = fmt_picker[0];\n",
       "\n",
       "    for (var ind in mpl.extensions) {\n",
       "        var fmt = mpl.extensions[ind];\n",
       "        var option = $(\n",
       "            '<option/>', {selected: fmt === mpl.default_extension}).html(fmt);\n",
       "        fmt_picker.append(option)\n",
       "    }\n",
       "\n",
       "    // Add hover states to the ui-buttons\n",
       "    $( \".ui-button\" ).hover(\n",
       "        function() { $(this).addClass(\"ui-state-hover\");},\n",
       "        function() { $(this).removeClass(\"ui-state-hover\");}\n",
       "    );\n",
       "\n",
       "    var status_bar = $('<span class=\"mpl-message\"/>');\n",
       "    nav_element.append(status_bar);\n",
       "    this.message = status_bar[0];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.request_resize = function(x_pixels, y_pixels) {\n",
       "    // Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,\n",
       "    // which will in turn request a refresh of the image.\n",
       "    this.send_message('resize', {'width': x_pixels, 'height': y_pixels});\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.send_message = function(type, properties) {\n",
       "    properties['type'] = type;\n",
       "    properties['figure_id'] = this.id;\n",
       "    this.ws.send(JSON.stringify(properties));\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.send_draw_message = function() {\n",
       "    if (!this.waiting) {\n",
       "        this.waiting = true;\n",
       "        this.ws.send(JSON.stringify({type: \"draw\", figure_id: this.id}));\n",
       "    }\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
       "    var format_dropdown = fig.format_dropdown;\n",
       "    var format = format_dropdown.options[format_dropdown.selectedIndex].value;\n",
       "    fig.ondownload(fig, format);\n",
       "}\n",
       "\n",
       "\n",
       "mpl.figure.prototype.handle_resize = function(fig, msg) {\n",
       "    var size = msg['size'];\n",
       "    if (size[0] != fig.canvas.width || size[1] != fig.canvas.height) {\n",
       "        fig._resize_canvas(size[0], size[1]);\n",
       "        fig.send_message(\"refresh\", {});\n",
       "    };\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_rubberband = function(fig, msg) {\n",
       "    var x0 = msg['x0'] / mpl.ratio;\n",
       "    var y0 = (fig.canvas.height - msg['y0']) / mpl.ratio;\n",
       "    var x1 = msg['x1'] / mpl.ratio;\n",
       "    var y1 = (fig.canvas.height - msg['y1']) / mpl.ratio;\n",
       "    x0 = Math.floor(x0) + 0.5;\n",
       "    y0 = Math.floor(y0) + 0.5;\n",
       "    x1 = Math.floor(x1) + 0.5;\n",
       "    y1 = Math.floor(y1) + 0.5;\n",
       "    var min_x = Math.min(x0, x1);\n",
       "    var min_y = Math.min(y0, y1);\n",
       "    var width = Math.abs(x1 - x0);\n",
       "    var height = Math.abs(y1 - y0);\n",
       "\n",
       "    fig.rubberband_context.clearRect(\n",
       "        0, 0, fig.canvas.width, fig.canvas.height);\n",
       "\n",
       "    fig.rubberband_context.strokeRect(min_x, min_y, width, height);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_figure_label = function(fig, msg) {\n",
       "    // Updates the figure title.\n",
       "    fig.header.textContent = msg['label'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_cursor = function(fig, msg) {\n",
       "    var cursor = msg['cursor'];\n",
       "    switch(cursor)\n",
       "    {\n",
       "    case 0:\n",
       "        cursor = 'pointer';\n",
       "        break;\n",
       "    case 1:\n",
       "        cursor = 'default';\n",
       "        break;\n",
       "    case 2:\n",
       "        cursor = 'crosshair';\n",
       "        break;\n",
       "    case 3:\n",
       "        cursor = 'move';\n",
       "        break;\n",
       "    }\n",
       "    fig.rubberband_canvas.style.cursor = cursor;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_message = function(fig, msg) {\n",
       "    fig.message.textContent = msg['message'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_draw = function(fig, msg) {\n",
       "    // Request the server to send over a new figure.\n",
       "    fig.send_draw_message();\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_image_mode = function(fig, msg) {\n",
       "    fig.image_mode = msg['mode'];\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.updated_canvas_event = function() {\n",
       "    // Called whenever the canvas gets updated.\n",
       "    this.send_message(\"ack\", {});\n",
       "}\n",
       "\n",
       "// A function to construct a web socket function for onmessage handling.\n",
       "// Called in the figure constructor.\n",
       "mpl.figure.prototype._make_on_message_function = function(fig) {\n",
       "    return function socket_on_message(evt) {\n",
       "        if (evt.data instanceof Blob) {\n",
       "            /* FIXME: We get \"Resource interpreted as Image but\n",
       "             * transferred with MIME type text/plain:\" errors on\n",
       "             * Chrome.  But how to set the MIME type?  It doesn't seem\n",
       "             * to be part of the websocket stream */\n",
       "            evt.data.type = \"image/png\";\n",
       "\n",
       "            /* Free the memory for the previous frames */\n",
       "            if (fig.imageObj.src) {\n",
       "                (window.URL || window.webkitURL).revokeObjectURL(\n",
       "                    fig.imageObj.src);\n",
       "            }\n",
       "\n",
       "            fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(\n",
       "                evt.data);\n",
       "            fig.updated_canvas_event();\n",
       "            fig.waiting = false;\n",
       "            return;\n",
       "        }\n",
       "        else if (typeof evt.data === 'string' && evt.data.slice(0, 21) == \"data:image/png;base64\") {\n",
       "            fig.imageObj.src = evt.data;\n",
       "            fig.updated_canvas_event();\n",
       "            fig.waiting = false;\n",
       "            return;\n",
       "        }\n",
       "\n",
       "        var msg = JSON.parse(evt.data);\n",
       "        var msg_type = msg['type'];\n",
       "\n",
       "        // Call the  \"handle_{type}\" callback, which takes\n",
       "        // the figure and JSON message as its only arguments.\n",
       "        try {\n",
       "            var callback = fig[\"handle_\" + msg_type];\n",
       "        } catch (e) {\n",
       "            console.log(\"No handler for the '\" + msg_type + \"' message type: \", msg);\n",
       "            return;\n",
       "        }\n",
       "\n",
       "        if (callback) {\n",
       "            try {\n",
       "                // console.log(\"Handling '\" + msg_type + \"' message: \", msg);\n",
       "                callback(fig, msg);\n",
       "            } catch (e) {\n",
       "                console.log(\"Exception inside the 'handler_\" + msg_type + \"' callback:\", e, e.stack, msg);\n",
       "            }\n",
       "        }\n",
       "    };\n",
       "}\n",
       "\n",
       "// from http://stackoverflow.com/questions/1114465/getting-mouse-location-in-canvas\n",
       "mpl.findpos = function(e) {\n",
       "    //this section is from http://www.quirksmode.org/js/events_properties.html\n",
       "    var targ;\n",
       "    if (!e)\n",
       "        e = window.event;\n",
       "    if (e.target)\n",
       "        targ = e.target;\n",
       "    else if (e.srcElement)\n",
       "        targ = e.srcElement;\n",
       "    if (targ.nodeType == 3) // defeat Safari bug\n",
       "        targ = targ.parentNode;\n",
       "\n",
       "    // jQuery normalizes the pageX and pageY\n",
       "    // pageX,Y are the mouse positions relative to the document\n",
       "    // offset() returns the position of the element relative to the document\n",
       "    var x = e.pageX - $(targ).offset().left;\n",
       "    var y = e.pageY - $(targ).offset().top;\n",
       "\n",
       "    return {\"x\": x, \"y\": y};\n",
       "};\n",
       "\n",
       "/*\n",
       " * return a copy of an object with only non-object keys\n",
       " * we need this to avoid circular references\n",
       " * http://stackoverflow.com/a/24161582/3208463\n",
       " */\n",
       "function simpleKeys (original) {\n",
       "  return Object.keys(original).reduce(function (obj, key) {\n",
       "    if (typeof original[key] !== 'object')\n",
       "        obj[key] = original[key]\n",
       "    return obj;\n",
       "  }, {});\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.mouse_event = function(event, name) {\n",
       "    var canvas_pos = mpl.findpos(event)\n",
       "\n",
       "    if (name === 'button_press')\n",
       "    {\n",
       "        this.canvas.focus();\n",
       "        this.canvas_div.focus();\n",
       "    }\n",
       "\n",
       "    var x = canvas_pos.x * mpl.ratio;\n",
       "    var y = canvas_pos.y * mpl.ratio;\n",
       "\n",
       "    this.send_message(name, {x: x, y: y, button: event.button,\n",
       "                             step: event.step,\n",
       "                             guiEvent: simpleKeys(event)});\n",
       "\n",
       "    /* This prevents the web browser from automatically changing to\n",
       "     * the text insertion cursor when the button is pressed.  We want\n",
       "     * to control all of the cursor setting manually through the\n",
       "     * 'cursor' event from matplotlib */\n",
       "    event.preventDefault();\n",
       "    return false;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
       "    // Handle any extra behaviour associated with a key event\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.key_event = function(event, name) {\n",
       "\n",
       "    // Prevent repeat events\n",
       "    if (name == 'key_press')\n",
       "    {\n",
       "        if (event.which === this._key)\n",
       "            return;\n",
       "        else\n",
       "            this._key = event.which;\n",
       "    }\n",
       "    if (name == 'key_release')\n",
       "        this._key = null;\n",
       "\n",
       "    var value = '';\n",
       "    if (event.ctrlKey && event.which != 17)\n",
       "        value += \"ctrl+\";\n",
       "    if (event.altKey && event.which != 18)\n",
       "        value += \"alt+\";\n",
       "    if (event.shiftKey && event.which != 16)\n",
       "        value += \"shift+\";\n",
       "\n",
       "    value += 'k';\n",
       "    value += event.which.toString();\n",
       "\n",
       "    this._key_event_extra(event, name);\n",
       "\n",
       "    this.send_message(name, {key: value,\n",
       "                             guiEvent: simpleKeys(event)});\n",
       "    return false;\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.toolbar_button_onclick = function(name) {\n",
       "    if (name == 'download') {\n",
       "        this.handle_save(this, null);\n",
       "    } else {\n",
       "        this.send_message(\"toolbar_button\", {name: name});\n",
       "    }\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.toolbar_button_onmouseover = function(tooltip) {\n",
       "    this.message.textContent = tooltip;\n",
       "};\n",
       "mpl.toolbar_items = [[\"Home\", \"Reset original view\", \"fa fa-home icon-home\", \"home\"], [\"Back\", \"Back to  previous view\", \"fa fa-arrow-left icon-arrow-left\", \"back\"], [\"Forward\", \"Forward to next view\", \"fa fa-arrow-right icon-arrow-right\", \"forward\"], [\"\", \"\", \"\", \"\"], [\"Pan\", \"Pan axes with left mouse, zoom with right\", \"fa fa-arrows icon-move\", \"pan\"], [\"Zoom\", \"Zoom to rectangle\", \"fa fa-square-o icon-check-empty\", \"zoom\"], [\"\", \"\", \"\", \"\"], [\"Download\", \"Download plot\", \"fa fa-floppy-o icon-save\", \"download\"]];\n",
       "\n",
       "mpl.extensions = [\"eps\", \"jpeg\", \"pdf\", \"png\", \"ps\", \"raw\", \"svg\", \"tif\"];\n",
       "\n",
       "mpl.default_extension = \"png\";var comm_websocket_adapter = function(comm) {\n",
       "    // Create a \"websocket\"-like object which calls the given IPython comm\n",
       "    // object with the appropriate methods. Currently this is a non binary\n",
       "    // socket, so there is still some room for performance tuning.\n",
       "    var ws = {};\n",
       "\n",
       "    ws.close = function() {\n",
       "        comm.close()\n",
       "    };\n",
       "    ws.send = function(m) {\n",
       "        //console.log('sending', m);\n",
       "        comm.send(m);\n",
       "    };\n",
       "    // Register the callback with on_msg.\n",
       "    comm.on_msg(function(msg) {\n",
       "        //console.log('receiving', msg['content']['data'], msg);\n",
       "        // Pass the mpl event to the overriden (by mpl) onmessage function.\n",
       "        ws.onmessage(msg['content']['data'])\n",
       "    });\n",
       "    return ws;\n",
       "}\n",
       "\n",
       "mpl.mpl_figure_comm = function(comm, msg) {\n",
       "    // This is the function which gets called when the mpl process\n",
       "    // starts-up an IPython Comm through the \"matplotlib\" channel.\n",
       "\n",
       "    var id = msg.content.data.id;\n",
       "    // Get hold of the div created by the display call when the Comm\n",
       "    // socket was opened in Python.\n",
       "    var element = $(\"#\" + id);\n",
       "    var ws_proxy = comm_websocket_adapter(comm)\n",
       "\n",
       "    function ondownload(figure, format) {\n",
       "        window.open(figure.imageObj.src);\n",
       "    }\n",
       "\n",
       "    var fig = new mpl.figure(id, ws_proxy,\n",
       "                           ondownload,\n",
       "                           element.get(0));\n",
       "\n",
       "    // Call onopen now - mpl needs it, as it is assuming we've passed it a real\n",
       "    // web socket which is closed, not our websocket->open comm proxy.\n",
       "    ws_proxy.onopen();\n",
       "\n",
       "    fig.parent_element = element.get(0);\n",
       "    fig.cell_info = mpl.find_output_cell(\"<div id='\" + id + \"'></div>\");\n",
       "    if (!fig.cell_info) {\n",
       "        console.error(\"Failed to find cell for figure\", id, fig);\n",
       "        return;\n",
       "    }\n",
       "\n",
       "    var output_index = fig.cell_info[2]\n",
       "    var cell = fig.cell_info[0];\n",
       "\n",
       "};\n",
       "\n",
       "mpl.figure.prototype.handle_close = function(fig, msg) {\n",
       "    var width = fig.canvas.width/mpl.ratio\n",
       "    fig.root.unbind('remove')\n",
       "\n",
       "    // Update the output cell to use the data from the current canvas.\n",
       "    fig.push_to_output();\n",
       "    var dataURL = fig.canvas.toDataURL();\n",
       "    // Re-enable the keyboard manager in IPython - without this line, in FF,\n",
       "    // the notebook keyboard shortcuts fail.\n",
       "    IPython.keyboard_manager.enable()\n",
       "    $(fig.parent_element).html('<img src=\"' + dataURL + '\" width=\"' + width + '\">');\n",
       "    fig.close_ws(fig, msg);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.close_ws = function(fig, msg){\n",
       "    fig.send_message('closing', msg);\n",
       "    // fig.ws.close()\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.push_to_output = function(remove_interactive) {\n",
       "    // Turn the data on the canvas into data in the output cell.\n",
       "    var width = this.canvas.width/mpl.ratio\n",
       "    var dataURL = this.canvas.toDataURL();\n",
       "    this.cell_info[1]['text/html'] = '<img src=\"' + dataURL + '\" width=\"' + width + '\">';\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.updated_canvas_event = function() {\n",
       "    // Tell IPython that the notebook contents must change.\n",
       "    IPython.notebook.set_dirty(true);\n",
       "    this.send_message(\"ack\", {});\n",
       "    var fig = this;\n",
       "    // Wait a second, then push the new image to the DOM so\n",
       "    // that it is saved nicely (might be nice to debounce this).\n",
       "    setTimeout(function () { fig.push_to_output() }, 1000);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._init_toolbar = function() {\n",
       "    var fig = this;\n",
       "\n",
       "    var nav_element = $('<div/>')\n",
       "    nav_element.attr('style', 'width: 100%');\n",
       "    this.root.append(nav_element);\n",
       "\n",
       "    // Define a callback function for later on.\n",
       "    function toolbar_event(event) {\n",
       "        return fig.toolbar_button_onclick(event['data']);\n",
       "    }\n",
       "    function toolbar_mouse_event(event) {\n",
       "        return fig.toolbar_button_onmouseover(event['data']);\n",
       "    }\n",
       "\n",
       "    for(var toolbar_ind in mpl.toolbar_items){\n",
       "        var name = mpl.toolbar_items[toolbar_ind][0];\n",
       "        var tooltip = mpl.toolbar_items[toolbar_ind][1];\n",
       "        var image = mpl.toolbar_items[toolbar_ind][2];\n",
       "        var method_name = mpl.toolbar_items[toolbar_ind][3];\n",
       "\n",
       "        if (!name) { continue; };\n",
       "\n",
       "        var button = $('<button class=\"btn btn-default\" href=\"#\" title=\"' + name + '\"><i class=\"fa ' + image + ' fa-lg\"></i></button>');\n",
       "        button.click(method_name, toolbar_event);\n",
       "        button.mouseover(tooltip, toolbar_mouse_event);\n",
       "        nav_element.append(button);\n",
       "    }\n",
       "\n",
       "    // Add the status bar.\n",
       "    var status_bar = $('<span class=\"mpl-message\" style=\"text-align:right; float: right;\"/>');\n",
       "    nav_element.append(status_bar);\n",
       "    this.message = status_bar[0];\n",
       "\n",
       "    // Add the close button to the window.\n",
       "    var buttongrp = $('<div class=\"btn-group inline pull-right\"></div>');\n",
       "    var button = $('<button class=\"btn btn-mini btn-primary\" href=\"#\" title=\"Stop Interaction\"><i class=\"fa fa-power-off icon-remove icon-large\"></i></button>');\n",
       "    button.click(function (evt) { fig.handle_close(fig, {}); } );\n",
       "    button.mouseover('Stop Interaction', toolbar_mouse_event);\n",
       "    buttongrp.append(button);\n",
       "    var titlebar = this.root.find($('.ui-dialog-titlebar'));\n",
       "    titlebar.prepend(buttongrp);\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._root_extra_style = function(el){\n",
       "    var fig = this\n",
       "    el.on(\"remove\", function(){\n",
       "\tfig.close_ws(fig, {});\n",
       "    });\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._canvas_extra_style = function(el){\n",
       "    // this is important to make the div 'focusable\n",
       "    el.attr('tabindex', 0)\n",
       "    // reach out to IPython and tell the keyboard manager to turn it's self\n",
       "    // off when our div gets focus\n",
       "\n",
       "    // location in version 3\n",
       "    if (IPython.notebook.keyboard_manager) {\n",
       "        IPython.notebook.keyboard_manager.register_events(el);\n",
       "    }\n",
       "    else {\n",
       "        // location in version 2\n",
       "        IPython.keyboard_manager.register_events(el);\n",
       "    }\n",
       "\n",
       "}\n",
       "\n",
       "mpl.figure.prototype._key_event_extra = function(event, name) {\n",
       "    var manager = IPython.notebook.keyboard_manager;\n",
       "    if (!manager)\n",
       "        manager = IPython.keyboard_manager;\n",
       "\n",
       "    // Check for shift+enter\n",
       "    if (event.shiftKey && event.which == 13) {\n",
       "        this.canvas_div.blur();\n",
       "        event.shiftKey = false;\n",
       "        // Send a \"J\" for go to next cell\n",
       "        event.which = 74;\n",
       "        event.keyCode = 74;\n",
       "        manager.command_mode();\n",
       "        manager.handle_keydown(event);\n",
       "    }\n",
       "}\n",
       "\n",
       "mpl.figure.prototype.handle_save = function(fig, msg) {\n",
       "    fig.ondownload(fig, null);\n",
       "}\n",
       "\n",
       "\n",
       "mpl.find_output_cell = function(html_output) {\n",
       "    // Return the cell and output element which can be found *uniquely* in the notebook.\n",
       "    // Note - this is a bit hacky, but it is done because the \"notebook_saving.Notebook\"\n",
       "    // IPython event is triggered only after the cells have been serialised, which for\n",
       "    // our purposes (turning an active figure into a static one), is too late.\n",
       "    var cells = IPython.notebook.get_cells();\n",
       "    var ncells = cells.length;\n",
       "    for (var i=0; i<ncells; i++) {\n",
       "        var cell = cells[i];\n",
       "        if (cell.cell_type === 'code'){\n",
       "            for (var j=0; j<cell.output_area.outputs.length; j++) {\n",
       "                var data = cell.output_area.outputs[j];\n",
       "                if (data.data) {\n",
       "                    // IPython >= 3 moved mimebundle to data attribute of output\n",
       "                    data = data.data;\n",
       "                }\n",
       "                if (data['text/html'] == html_output) {\n",
       "                    return [cell, data, j];\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    }\n",
       "}\n",
       "\n",
       "// Register the function which deals with the matplotlib target/channel.\n",
       "// The kernel may be null if the page has been refreshed.\n",
       "if (IPython.notebook.kernel != null) {\n",
       "    IPython.notebook.kernel.comm_manager.register_target('matplotlib', mpl.mpl_figure_comm);\n",
       "}\n"
      ],
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<img src=\"\" width=\"640\">"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x246061774e0>"
      ]
     },
     "execution_count": 371,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig,ax = plt.subplots(1,1)\n",
    "rebuy_rates.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 429,
   "metadata": {},
   "outputs": [],
   "source": [
    "del mdata['one']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 430,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>count</th>\n",
       "      <th>price</th>\n",
       "      <th>single_price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buy_date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>11.77</td>\n",
       "      <td>11.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>12.00</td>\n",
       "      <td>12.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>77.00</td>\n",
       "      <td>15.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "      <td>10.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-03</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "      <td>10.38</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          user_id  count  price  single_price\n",
       "buy_date                                     \n",
       "1997-01         1      1  11.77         11.77\n",
       "1997-01         2      1  12.00         12.00\n",
       "1997-01         2      5  77.00         15.40\n",
       "1997-01         3      2  20.76         10.38\n",
       "1997-03         3      2  20.76         10.38"
      ]
     },
     "execution_count": 430,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mdata.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 471,
   "metadata": {},
   "outputs": [],
   "source": [
    "mdata.set_index('buy_date',inplace=True)\n",
    "mdata['flag'] = 1\n",
    "ndata = mdata.set_index(mdata.index - 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 472,
   "metadata": {},
   "outputs": [],
   "source": [
    "ndata = ndata.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 473,
   "metadata": {},
   "outputs": [],
   "source": [
    "mdata = mdata.reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 480,
   "metadata": {},
   "outputs": [],
   "source": [
    "month_reserve_rate = pd.merge(mdata[['buy_date','user_id','flag']],ndata[['buy_date','user_id','flag']],on=['buy_date','user_id'],how='left').fillna(0).groupby('buy_date').agg(lambda g:g.flag_y.sum()/g.flag_x.sum())['user_id']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 482,
   "metadata": {},
   "outputs": [],
   "source": [
    "mdata.set_index('buy_date',inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 492,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>count</th>\n",
       "      <th>price</th>\n",
       "      <th>single_price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buy_date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>11.77</td>\n",
       "      <td>11.77</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>12.00</td>\n",
       "      <td>12.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>77.00</td>\n",
       "      <td>15.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "      <td>10.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-03</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>20.76</td>\n",
       "      <td>10.38</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          user_id  count  price  single_price\n",
       "buy_date                                     \n",
       "1997-01         1      1  11.77         11.77\n",
       "1997-01         2      1  12.00         12.00\n",
       "1997-01         2      5  77.00         15.40\n",
       "1997-01         3      2  20.76         10.38\n",
       "1997-03         3      2  20.76         10.38"
      ]
     },
     "execution_count": 492,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mdata.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 496,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x246061774e0>"
      ]
     },
     "execution_count": 496,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "month_reserve_rate.plot()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 497,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>count</th>\n",
       "      <th>price</th>\n",
       "      <th>single_price</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>buy_date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>6804</td>\n",
       "      <td>2</td>\n",
       "      <td>27.48</td>\n",
       "      <td>13.740000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>5026</td>\n",
       "      <td>2</td>\n",
       "      <td>25.98</td>\n",
       "      <td>12.990000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>1556</td>\n",
       "      <td>2</td>\n",
       "      <td>28.48</td>\n",
       "      <td>14.240000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>21182</td>\n",
       "      <td>1</td>\n",
       "      <td>14.49</td>\n",
       "      <td>14.490000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>561</td>\n",
       "      <td>2</td>\n",
       "      <td>16.48</td>\n",
       "      <td>8.240000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>561</td>\n",
       "      <td>1</td>\n",
       "      <td>23.99</td>\n",
       "      <td>23.990000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>17314</td>\n",
       "      <td>2</td>\n",
       "      <td>22.98</td>\n",
       "      <td>11.490000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>15429</td>\n",
       "      <td>1</td>\n",
       "      <td>15.49</td>\n",
       "      <td>15.490000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>15429</td>\n",
       "      <td>1</td>\n",
       "      <td>17.99</td>\n",
       "      <td>17.990000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>22085</td>\n",
       "      <td>5</td>\n",
       "      <td>61.82</td>\n",
       "      <td>12.364000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>5034</td>\n",
       "      <td>3</td>\n",
       "      <td>89.97</td>\n",
       "      <td>29.990000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>1551</td>\n",
       "      <td>8</td>\n",
       "      <td>123.92</td>\n",
       "      <td>15.490000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>9993</td>\n",
       "      <td>2</td>\n",
       "      <td>37.48</td>\n",
       "      <td>18.740000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>22796</td>\n",
       "      <td>3</td>\n",
       "      <td>32.47</td>\n",
       "      <td>10.823333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>14210</td>\n",
       "      <td>6</td>\n",
       "      <td>79.71</td>\n",
       "      <td>13.285000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>18954</td>\n",
       "      <td>2</td>\n",
       "      <td>23.98</td>\n",
       "      <td>11.990000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>22802</td>\n",
       "      <td>4</td>\n",
       "      <td>62.51</td>\n",
       "      <td>15.627500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>5026</td>\n",
       "      <td>1</td>\n",
       "      <td>13.49</td>\n",
       "      <td>13.490000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>18952</td>\n",
       "      <td>3</td>\n",
       "      <td>36.97</td>\n",
       "      <td>12.323333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>22805</td>\n",
       "      <td>2</td>\n",
       "      <td>31.48</td>\n",
       "      <td>15.740000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>559</td>\n",
       "      <td>4</td>\n",
       "      <td>54.85</td>\n",
       "      <td>13.712500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>16040</td>\n",
       "      <td>3</td>\n",
       "      <td>38.47</td>\n",
       "      <td>12.823333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>17725</td>\n",
       "      <td>3</td>\n",
       "      <td>44.47</td>\n",
       "      <td>14.823333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>7677</td>\n",
       "      <td>2</td>\n",
       "      <td>37.50</td>\n",
       "      <td>18.750000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>19909</td>\n",
       "      <td>7</td>\n",
       "      <td>80.93</td>\n",
       "      <td>11.561429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>5958</td>\n",
       "      <td>1</td>\n",
       "      <td>12.58</td>\n",
       "      <td>12.580000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>5958</td>\n",
       "      <td>3</td>\n",
       "      <td>37.04</td>\n",
       "      <td>12.346667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>10793</td>\n",
       "      <td>1</td>\n",
       "      <td>11.49</td>\n",
       "      <td>11.490000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>23275</td>\n",
       "      <td>1</td>\n",
       "      <td>31.49</td>\n",
       "      <td>31.490000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-06</th>\n",
       "      <td>17314</td>\n",
       "      <td>2</td>\n",
       "      <td>28.98</td>\n",
       "      <td>14.490000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5004</td>\n",
       "      <td>1</td>\n",
       "      <td>26.77</td>\n",
       "      <td>26.770000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5003</td>\n",
       "      <td>2</td>\n",
       "      <td>18.54</td>\n",
       "      <td>9.270000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5002</td>\n",
       "      <td>2</td>\n",
       "      <td>9.98</td>\n",
       "      <td>4.990000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5000</td>\n",
       "      <td>2</td>\n",
       "      <td>29.73</td>\n",
       "      <td>14.865000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5009</td>\n",
       "      <td>1</td>\n",
       "      <td>26.99</td>\n",
       "      <td>26.990000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>4999</td>\n",
       "      <td>5</td>\n",
       "      <td>71.25</td>\n",
       "      <td>14.250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>4998</td>\n",
       "      <td>1</td>\n",
       "      <td>12.99</td>\n",
       "      <td>12.990000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>4997</td>\n",
       "      <td>2</td>\n",
       "      <td>29.58</td>\n",
       "      <td>14.790000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>4996</td>\n",
       "      <td>1</td>\n",
       "      <td>15.36</td>\n",
       "      <td>15.360000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>4996</td>\n",
       "      <td>1</td>\n",
       "      <td>14.96</td>\n",
       "      <td>14.960000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>4995</td>\n",
       "      <td>1</td>\n",
       "      <td>17.90</td>\n",
       "      <td>17.900000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5008</td>\n",
       "      <td>3</td>\n",
       "      <td>41.91</td>\n",
       "      <td>13.970000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5010</td>\n",
       "      <td>2</td>\n",
       "      <td>25.74</td>\n",
       "      <td>12.870000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5024</td>\n",
       "      <td>1</td>\n",
       "      <td>27.99</td>\n",
       "      <td>27.990000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5018</td>\n",
       "      <td>1</td>\n",
       "      <td>13.77</td>\n",
       "      <td>13.770000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5023</td>\n",
       "      <td>1</td>\n",
       "      <td>15.99</td>\n",
       "      <td>15.990000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5022</td>\n",
       "      <td>3</td>\n",
       "      <td>38.71</td>\n",
       "      <td>12.903333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5021</td>\n",
       "      <td>2</td>\n",
       "      <td>26.14</td>\n",
       "      <td>13.070000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5021</td>\n",
       "      <td>1</td>\n",
       "      <td>13.97</td>\n",
       "      <td>13.970000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5020</td>\n",
       "      <td>2</td>\n",
       "      <td>39.98</td>\n",
       "      <td>19.990000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5019</td>\n",
       "      <td>1</td>\n",
       "      <td>15.36</td>\n",
       "      <td>15.360000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5017</td>\n",
       "      <td>1</td>\n",
       "      <td>15.36</td>\n",
       "      <td>15.360000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5010</td>\n",
       "      <td>1</td>\n",
       "      <td>12.77</td>\n",
       "      <td>12.770000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5016</td>\n",
       "      <td>3</td>\n",
       "      <td>43.30</td>\n",
       "      <td>14.433333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5015</td>\n",
       "      <td>2</td>\n",
       "      <td>28.74</td>\n",
       "      <td>14.370000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5014</td>\n",
       "      <td>1</td>\n",
       "      <td>14.79</td>\n",
       "      <td>14.790000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5013</td>\n",
       "      <td>1</td>\n",
       "      <td>13.97</td>\n",
       "      <td>13.970000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5012</td>\n",
       "      <td>8</td>\n",
       "      <td>134.55</td>\n",
       "      <td>16.818750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>5011</td>\n",
       "      <td>1</td>\n",
       "      <td>14.37</td>\n",
       "      <td>14.370000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-01</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>11.77</td>\n",
       "      <td>11.770000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>67663 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          user_id  count   price  single_price\n",
       "buy_date                                      \n",
       "1998-06      6804      2   27.48     13.740000\n",
       "1998-06      5026      2   25.98     12.990000\n",
       "1998-06      1556      2   28.48     14.240000\n",
       "1998-06     21182      1   14.49     14.490000\n",
       "1998-06       561      2   16.48      8.240000\n",
       "1998-06       561      1   23.99     23.990000\n",
       "1998-06     17314      2   22.98     11.490000\n",
       "1998-06     15429      1   15.49     15.490000\n",
       "1998-06     15429      1   17.99     17.990000\n",
       "1998-06     22085      5   61.82     12.364000\n",
       "1998-06      5034      3   89.97     29.990000\n",
       "1998-06      1551      8  123.92     15.490000\n",
       "1998-06      9993      2   37.48     18.740000\n",
       "1998-06     22796      3   32.47     10.823333\n",
       "1998-06     14210      6   79.71     13.285000\n",
       "1998-06     18954      2   23.98     11.990000\n",
       "1998-06     22802      4   62.51     15.627500\n",
       "1998-06      5026      1   13.49     13.490000\n",
       "1998-06     18952      3   36.97     12.323333\n",
       "1998-06     22805      2   31.48     15.740000\n",
       "1998-06       559      4   54.85     13.712500\n",
       "1998-06     16040      3   38.47     12.823333\n",
       "1998-06     17725      3   44.47     14.823333\n",
       "1998-06      7677      2   37.50     18.750000\n",
       "1998-06     19909      7   80.93     11.561429\n",
       "1998-06      5958      1   12.58     12.580000\n",
       "1998-06      5958      3   37.04     12.346667\n",
       "1998-06     10793      1   11.49     11.490000\n",
       "1998-06     23275      1   31.49     31.490000\n",
       "1998-06     17314      2   28.98     14.490000\n",
       "...           ...    ...     ...           ...\n",
       "1997-01      5004      1   26.77     26.770000\n",
       "1997-01      5003      2   18.54      9.270000\n",
       "1997-01      5002      2    9.98      4.990000\n",
       "1997-01      5000      2   29.73     14.865000\n",
       "1997-01      5009      1   26.99     26.990000\n",
       "1997-01      4999      5   71.25     14.250000\n",
       "1997-01      4998      1   12.99     12.990000\n",
       "1997-01      4997      2   29.58     14.790000\n",
       "1997-01      4996      1   15.36     15.360000\n",
       "1997-01      4996      1   14.96     14.960000\n",
       "1997-01      4995      1   17.90     17.900000\n",
       "1997-01      5008      3   41.91     13.970000\n",
       "1997-01      5010      2   25.74     12.870000\n",
       "1997-01      5024      1   27.99     27.990000\n",
       "1997-01      5018      1   13.77     13.770000\n",
       "1997-01      5023      1   15.99     15.990000\n",
       "1997-01      5022      3   38.71     12.903333\n",
       "1997-01      5021      2   26.14     13.070000\n",
       "1997-01      5021      1   13.97     13.970000\n",
       "1997-01      5020      2   39.98     19.990000\n",
       "1997-01      5019      1   15.36     15.360000\n",
       "1997-01      5017      1   15.36     15.360000\n",
       "1997-01      5010      1   12.77     12.770000\n",
       "1997-01      5016      3   43.30     14.433333\n",
       "1997-01      5015      2   28.74     14.370000\n",
       "1997-01      5014      1   14.79     14.790000\n",
       "1997-01      5013      1   13.97     13.970000\n",
       "1997-01      5012      8  134.55     16.818750\n",
       "1997-01      5011      1   14.37     14.370000\n",
       "1997-01         1      1   11.77     11.770000\n",
       "\n",
       "[67663 rows x 4 columns]"
      ]
     },
     "execution_count": 497,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mdata.sort_index(ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 498,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: DeprecationWarning: \n",
      ".ix is deprecated. Please use\n",
      ".loc for label based indexing or\n",
      ".iloc for positional indexing\n",
      "\n",
      "See the documentation here:\n",
      "http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    }
   ],
   "source": [
    "close_buy = mdata.ix[mdata.index.unique()[:3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 499,
   "metadata": {},
   "outputs": [],
   "source": [
    "close_buy = close_buy[close_buy['count'] > 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 500,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(12373, 4)"
      ]
     },
     "execution_count": 500,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "close_buy.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 505,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5634041865351976"
      ]
     },
     "execution_count": 505,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "close_buy[close_buy['count'] > 2].shape[0] / close_buy.shape[0] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 507,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7749246879931143"
      ]
     },
     "execution_count": 507,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "close_buy[close_buy['count'] == 2].shape[0] / close_buy[close_buy['count'] > 2].shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 508,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    12373.000000\n",
       "mean         3.481694\n",
       "std          2.476549\n",
       "min          2.000000\n",
       "25%          2.000000\n",
       "50%          3.000000\n",
       "75%          4.000000\n",
       "max         43.000000\n",
       "Name: count, dtype: float64"
      ]
     },
     "execution_count": 508,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "close_buy['count'].describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
